我知道如何设置enable = YES / NO,一个使用属性和xib创建的按钮。但是,如何对同一类中另一个方法的编程创建按钮执行相同的操作?
例如,这是viewDidLoad中的我的按钮:
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:@"allModsBtn.png"];
[AllList setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:AllList];
// add targets and actions
[AllList addTarget:self action:@selector(getButtons:) forControlEvents:UIControlEventTouchUpInside];
AllList.tag = 0;
我想在另一种方法中将此按钮的启用设置为YES或NO。
答案 0 :(得分:1)
@implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.tag = 121;
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:@"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:@selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
OR
//In this case you dont need to define uibutton to globaly
UIButton *button = (UIButton*)[[self view] viewWithTag:121];
[button setEnabled:YES];
}
答案 1 :(得分:0)
您必须将该按钮设为视图控制器的ivar。
@implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:@"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:@selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
}
答案 2 :(得分:0)
这样的事情:
UIButton *myButton = [self.view viewWithTag:BUTTON_TAG]; // in your case is 0
[myButton setEnabled:YES];
答案 3 :(得分:0)
两种方法:
1)你可以把它变成ivar然后
AllList.enabled = YES;
或
[AllList setEnabled:YES];
2) 为按钮设置唯一标签
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
AllList.tag = kUNIQUE_TAG;
在你要弄乱按钮的启用属性
的方法中UIButton *theButton = (UIButton *)[self viewWithTag:kUNIQUE_TAG];
[theButton setEnabled:YES];