0

我创建了一个自定义 UIView (ProgressView),在其中绘制了通过 StyleKit 从 PaintCode 导入的形状。

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

进度视图.h

#import <UIKit/UIKit.h>

@interface ProogressView : UIView

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


@end

进度视图.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;
}

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

*ViewController.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
4

1 回答 1

1

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

[self.view addSubview:progressView];

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

[progressView setFrame:self.view.bounds];

您可以在 viewDid/WillLayoutSubviews 方法中执行此操作,以在旋转/窗口调整大小事件中更改它。

顺便说一句,不要将您的视图命名为new,这太可怕了。这样的名称甚至不能说明它是什么类型的变量。

于 2015-11-01T19:50:09.287 回答