2015-11-01 85 views
0

我创建了一个自定义的UIView(ProgressView),我绘制从PaintCode通过StyleKit导入的形状。如何从ViewController更改自定义UIView的变量

以下是代码。我已经在我的自定义UIView中声明了实例变量属性,当我尝试从ViewController更改属性时,它不起作用。

ProgressView.h

#import <UIKit/UIKit.h> 

@interface ProogressView : UIView 

@property (nonatomic) float daysFraction; 
@property (nonatomic) float pagesFraction; 


@end 

ProgressView.m

#import "ProgressView.h" 
#import "StyleKitName.h" 
#import "ViewController.h" 

@implementation ProgressView 
@synthesize daysFraction = _daysFraction; 
@synthesize pagesFraction = _pagesFraction; 


- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    [StyleKitName drawCanvas1WithDaysFraction:self.daysFraction pageFraction:self.pagesFraction]; 
} 


-(void)awakeFromNib { 
    [super awakeFromNib]; 
    self.pagesFraction = 0; 
    self.daysFraction = 0; 
} 

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@end 

*查看Controller.m或者**

#import "ViewController.h" 
#import "ButtonAnimation.h" 
#import "ProgressView.h" 
#import "StyleKitName.h" 

@interface ViewController() 

@property (weak, nonatomic) IBOutlet ButtonAnimation *buttonView; 
@property (weak, nonatomic) IBOutlet UIButton *actionButton; 

@end 


@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    ProgressView *new =[[ ProgressView alloc]init]; 
    new.daysFraction = 0.7f; // here I am setting value to variable in custom view ProgressView but not working. 

} 

- (IBAction)animateTheButton:(id)sender { 
    self.buttonView.layer.backgroundColor = [UIColor clearColor].CGColor; 
    [self.buttonView addErrorAnimation]; 

} 


@end 
+2

您已经在viewDidLoad中创建了一个新的进度视图,但未将其添加到您的视图层次结构中,因此它将不可见。并且引用存储在一个局部变量中,因此只要需要退出就会被释放。如果您通过故事板屏幕在屏幕上显示另一个实例,那么您应该设置一个IBOutlet属性,以便您可以更新*该实例 – Paulw11

+0

我不是100%确定,但我不认为您可以使用“new”作为变量名称。在Objective-C中可能有一个关键字,如果没有的话,我不认为它是一个好的变量名称:) – Yan

+0

没有理由不能设置IBAction方法的值。也许显示代码。 – Paulw11

回答

1

您需要将此视图添加到UIViewController的看法:

[self.view addSubview:progressView]; 

之后,还必须设置一个框架。例如

[progressView setFrame:self.view.bounds]; 

你可以做到这一点在viewDid/WillLayoutSubviews方法来改变它的旋转/窗口大小调整事件。

顺便说一句,不要将您的视图命名为new,这太可怕了。这个名字甚至没有告诉它是什么样的变量。