开发者

changing a button's text from another method when the buttons are in a subclass?

How do you access a button that's in a subview of a view?

I have a viewController 'GamePlay', which has a scrollview in it called 'gameScroll'. Within this scrollview I have about 100 buttons, each with a tag, and I want to be able to change the text of the button from another method.

- (void) viewDidLoad {
  //Created buttons in a for-loop, assigning each a tag
    //numOfButtons is total number of buttons created
}

I imagine it would be something like this? but I cant seem to find an answer on exactly how i do it when the button is in a subv开发者_如何学编程iew

- (void) otherMethod {

  for (int i=0; i<numOfButtons; i++) {
    // tagsForAction = get list of buttons that need to be changed from another array

    for (j=0; j<tagsForAction.length; j++) {
      intTagForAction = [tagsForAction objectAtIndex:j];

      if (i = tagForAction) {
        UIButton* button = [Gameplay.gameScroll.view viewWithTag:tagForAction];
        button.title = @"A";
      }    
    }
  }
}

I know this code isnt totally right. Im just giving you an idea of the process. I can do everything except this part in the if statement:

UIButton* button = [Gameplay.gameScroll.view viewWithTag:tagForAction];
button.title = @"A";

so how do I change the text of these buttons?


NSArray *subViewList = [gameScroll subviews];
for (id button in subViewList)
{
    if ([button isKindOfClass:[UIButton class]]) 
    {
                [button setTitle:@"OK" forState:UIControlStateNormal];
            }
    }

Try this code


You don't have to use the for loop like that, you can get an array of subviews from any view by doing something like [someView subviews]. So, inside of your otherMethod function you can do something like:

for (UIView *v in [gameScroll subviews]) {
    if (v.tag == <some_int_here>) {
        [v setTitle:@"Some other title" forState:UIControlStateNormal];
    }
}

I'm not exactly sure what your criteria is for determining which buttons inside the gameScroll view have to get updated, but you can work from here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜