当我第二次进入应用程序时,仍然按下两个按钮

时间:2013-07-14 14:14:19

标签: ios if-statement uibutton

当我按Btn1Btn2启用了用户互动并执行操作时,我的应用中有两个按钮(例如Btn1Btn2)。这没有问题。

现在的问题是,

我希望按钮恢复到开始时的默认状态(当我按Btn1``Btn2启用用户交互并执行操作时)

感谢任何帮助。

以下是代码:

- (IBAction)Btn1:(id)sender;    
{
    // if the Btn1 pressed enable the Btn2
    if (...)
    {
        button.enabled = YES;   
    }
}

- (IBAction)Btn2:(id)sender;
{
    if (button.enabled == YES) 
        // do any action in here    
    } 
}

2 个答案:

答案 0 :(得分:0)

在函数viewWillAppear中设置默认状态。

这样的事情:

.h头文件

@property (nonatomic, weak) IBOutlet UIButton *button1; // connect to the button in interface builder
@property (nonatomic, weak) IBOutlet UIButton *button2; // connect to the button in interface builder

-(IBAction)buttonPressed:(UIButton*)button; // set this as an action for both buttons

.m文件

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated]
    [self defaultState];
}

-(IBAction)buttonPressed:(UIButton*)button;   
{
    if (button == self.button1) { // button 1 pressed        
       self.button2.enabled = YES;   
    } else if (button == self.button2) { // button 2 was pressed
       [self defaultState]; // go back to default state
    }
}

-(void)defaultState {  // your default state of the app
   self.button2.enabled = NO;
   // other stuff
}

答案 1 :(得分:0)

您可以向视图控制器添加便捷方法resetButtonStates,并以两种方式调用它:

  1. -(void)viewWillAppear:(BOOL)animated
  2. 每当您的应用发布UIApplicationDidBecomeActiveNotification时。这是因为当您将应用程序带到前台时,不会调用viewWillAppear:。使用NSNotifcationCenter注册此通知(有关详细信息,请参阅文档)。
  3. resetButtonStates中,您应该将按钮更新为适当的状态(启用/禁用/等)。