开发者

iPhone - How to dynamically load/unload buttons onto view

开发者 https://www.devze.com 2023-02-02 06:01 出处:网络
I\'m writing an app that dynamically loads buttons and then, based on the changes in app settings, previous buttons will be unloaded and new ones will be loaded. Is there a best practice for doing tha

I'm writing an app that dynamically loads buttons and then, based on the changes in app settings, previous buttons will be unloaded and new ones will be loaded. Is there a best practice for doing that? I'm running into several issues.. I posted on another forum but I didn't get a resolution. I 开发者_运维技巧research the documentation but the key problem is that it's not unloading the old buttons and loading the new ones.

The algorithm that I follow is:

  1. In my viewDidLoad --> call reloadButtons (this positions all buttons on the view)
  2. When user makes changes in application settings (within the application), I call: [self unloadButtons]; [self reloadButtons];

The code:

- (void)reloadButtons 
{

  //some code

  xIncrement = 50; //for positioning buttons 
  yIncrement = 50;

  x = 25;
  y = 25;

  for (int i = 1; i <= numOfButtons; i++) {
    tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 50, 50)];

    x += xIncrement;
    if (i % 5 == 0) { //if this is the 6th button on the line then go to the next line (increment y)
       x = 25;
       y += yIncrement;
    }

    [tmpButton setTitle:@"title" forState:UIControlStateNormal];
    [tmpButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];

    [tmpButton setFont:[UIFont boldSystemFontOfSize:24.0]];

    UIImage * bgImage = [UIImage imageNamed:@"image.png"];
    [tmpButton setBackgroundImage:bgImage forState:UIControlStateNormal];


    [tmpButton addTarget:self action:@selector(tapButton forControlEvents:UIControlEventTouchUpInside];
tmpButton.tag = i + 50;

    [self.view addSubview:tmpButton];

    [tmpButton release];

  } //for loop

  prevNumOfbuttons = numOfButtons; //saves the number of previous buttons since this may change depending on the user's choice in the application settings.

} //reloadButtons

Upon exit from the settings page, I run this unloadButtons function then follow it with the reloadButtons again.. this is where I'm running into the issues.. see my comments within the code below:

- (void)unloadButtons {

   NSLog(@"unloadButtons");

   NSArray *arrSubViews = [self.view subviews];
   int count = [arrSubViews count];

   for (int i = 0; i < count; i++) {
     UIView *vv = [arrSubViews objectAtIndex:i];

     if ([vv isMemberOfClass:[UIButton class]]) {
        NSInteger buttonTag = [vv tag];
        if (buttonTag < 200) { //all buttons that were added in reloadButtons have a tag < 200 - this is to distinguish them from the info button, which was assigned a tag of 203

           NSLog(@"Button Caption: %@", [vv currentTitle]);
           [vv removeFromSuperview];

        }

     }
   } //for loop

[arrSubViews release];

[self.view setNeedsDisplay]; //I added this to see if the reason that the old buttons are not going away is because the view is not refreshing

} //unloadButtons

I appreciate any help!!

-Al

0

精彩评论

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