Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone-如何在视图上动态加载/卸载按钮_Iphone_Dynamic_Button - Fatal编程技术网

iPhone-如何在视图上动态加载/卸载按钮

iPhone-如何在视图上动态加载/卸载按钮,iphone,dynamic,button,Iphone,Dynamic,Button,我正在编写一个动态加载按钮的应用程序,然后根据应用程序设置中的更改,卸载以前的按钮并加载新按钮。有这样做的最佳实践吗?我遇到了几个问题。。我在另一个论坛上发了帖子,但没有得到决议。我研究了文档,但关键问题是它没有卸载旧按钮并加载新按钮 我遵循的算法是: 在我的视图中,DIDLOAD-->调用重载按钮(这将在视图上定位所有按钮) 当用户更改应用程序设置(在应用程序内)时,我调用: [自动卸载按钮]; [自动重新加载按钮] 守则: - (void)reloadButtons { //some

我正在编写一个动态加载按钮的应用程序,然后根据应用程序设置中的更改,卸载以前的按钮并加载新按钮。有这样做的最佳实践吗?我遇到了几个问题。。我在另一个论坛上发了帖子,但没有得到决议。我研究了文档,但关键问题是它没有卸载旧按钮并加载新按钮

我遵循的算法是:

  • 在我的视图中,DIDLOAD-->调用重载按钮(这将在视图上定位所有按钮)
  • 当用户更改应用程序设置(在应用程序内)时,我调用: [自动卸载按钮]; [自动重新加载按钮] 守则:

    - (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
    
    -(无效)重新加载按钮
    {
    //一些代码
    xIncrement=50;//用于定位按钮
    yIncrement=50;
    x=25;
    y=25;
    
    for(int i=1;我不发布子视图。您没有创建它,只是请求它。谢谢,我这样做了,但这并没有解决为什么旧按钮不会消失而新按钮会出现的问题。