无法设置对象 - 只读属性或找不到setter

She*_*lam 1 iphone cocoa cocoa-touch objective-c iphone-sdk-3.0

我无法简单地为UI按钮分配值:

//Assume rect is defined
rect = CGRectMake(13, 10, 48, 48);
profileButton = [[UIButton alloc] initWithFrame:rect];
profileButton.buttonType = UIButtonTypeCustom;
Run Code Online (Sandbox Code Playgroud)

在尝试将buttonType分配给UIButtonTypeCustom时,我得到"无法设置对象 - 只读属性或找到setter".

Ste*_*ntz 5

这是因为它buttonType是一个只读属性.您只能使用创建特定类型的按钮buttonWithType:.

profileButton = [[UIButton buttonWithType: UIButtonTypeCustom] retain];
profileButton.frame = CGRectMake(13, 10, 48, 48);
Run Code Online (Sandbox Code Playgroud)

(不知道是什么profileButton,但假设它不是保留财产)