尝试从类文件创建按钮时找不到属性视图

时间:2014-02-01 08:23:57

标签: ios objective-c

我不确定我做错了什么。我使用下面的脚本在视图控制器中创建了按钮。但我想在不同的页面上创建许多相同的按钮,所以我想创建自己的自定义类。但是我添加子视图的行不会让我访问我的视图。有什么建议吗?

- (void)makeButtonWithTitle:(NSString *)title atPositionIndex:(NSInteger)index {
    const CGFloat buttonHeight = 60.0f;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(.0f, buttonHeight*index, 300.0f, buttonHeight)];

    button.tag = index;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.titleLabel.numberOfLines = 0; // Dynamic number of lines
    button.titleLabel.textColor = [UIColor blackColor];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

1 个答案:

答案 0 :(得分:1)

将方法调用更改为:

 - (void)makeButtonWithTitle:(NSString*)title atPositionIndex:(NSInteger)index view2add:(UIView*)view2add

然后在方法结束时:[view2add addSubview:button];