2013-07-14 31 views
0

當我按Btn1,Btn2啓用用戶交互並執行操作時,我的應用程序中有兩個按鈕(說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  
    } 
} 
+0

首先,必須啓用一個按鈕才能夠按下。如果你想發起'Btn2:(id)發送者動作,那麼使用'performSelector'。 – Souljacker

回答

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 
} 
+0

我有一個想法,如果我讓整個視圖從** Btn2 **打開並按下後開始。因爲我不想只是按鈕被取消啓用我想要按鈕內的動作2.有任何代碼可以幫助我使視圖從** Btn2 **按下後從開始開始? –

+0

你能解釋我你在做什麼? –

+0

即時通訊嘗試休息按鈕狀態像開始時,我開始應用程序 –

0

你可以添加一個便捷方法resetButtonStates到您的視圖控制器,並將其稱爲兩種方式:

  1. -(void)viewWillAppear:(BOOL)animated
  2. 每當UIApplicationDidBecomeActiveNotification由您的應用程序發佈。這是因爲在將應用程序放到前臺時不會調用viewWillAppear:。使用NSNotifcationCenter註冊此通知(請參閱文檔以獲取更多信息)。

resetButtonStates你應該更新按鈕到適當的狀態(啓用/禁用/等)。

+0

如果你可以寫代碼,因爲我迷惑小 –

相關問題