0

プロパティとxibを使用して作成されたボタンであるenable=YES/NOを設定する方法を知っています。しかし、同じクラスの別のメソッドからプログラムで作成されたボタンに対して同じことをどのように行いますか?

たとえば、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に設定したいと思います。

4

4 に答える 4

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];

}
于 2012-10-17T18:57:05.120 に答える
0

そのボタンをViewControllerの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;
}
于 2012-10-17T15:54:55.050 に答える
0

このようなもの:

UIButton *myButton = [self.view viewWithTag:BUTTON_TAG]; // in your case is 0
[myButton setEnabled:YES];
于 2012-10-17T15:55:10.790 に答える
0

それを行う2つの方法:

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];
于 2012-10-17T16:02:06.737 に答える