无法为readonly属性UIButton赋值

时间:2018-04-17 09:31:08

标签: objective-c

我刚开始使用Objective C进行ios开发。我正在尝试开发一个带有标签和按钮的简单应用程序。当单击按钮时,按钮的标签内容和标题应该更改。但是,当我尝试更改UIButton的标题时,我收到了错误

Error :Assignment to readonly property

这是我的基本代码

ViewController.h

@interface ViewController : UIViewController
@property(nonatomic , weak) IBOutlet UILabel* labelName;
@property(nonatomic , weak) IBOutlet UIButton* greetButton;

-(IBAction)greetClickedAction:(id)sender; //Button connected here to get the click event
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)greetClickedAction:(id)sender
{
    _labelName.text  = @"Label Changed";    //This Works fine
    greetButton.currentTitle=@"New Title";  //Error :Assignment to readonly property
}

@end

我的问题是为什么我在向UIButton分配内容而不是UILabel时收到错误?我怎样才能解决这个问题 ?

更新

我可以使用@dasblinkenlight建议通过执行以下操作来删除readonly错误,但代码仍然不会更改按钮的标题

_greetButton.titleLabel.text = @"New Title";

2 个答案:

答案 0 :(得分:1)

greetButton.currentTitle用于检查按钮上的文字。它不能直接设置,因为它是计算的。您可以设置greetButton.titleLabel的文字属性,或者改为拨打setTitle:forState:

[_greetButton setTitle: @"New Title" forState: UIControlStateNormal];

答案 1 :(得分:0)

请尝试以下一次:

filledcurves