2011-03-02 25 views
1

在我的應用程序中我調用UIView,並且UIView就像我的應用程序中的設置屏幕,我在UIView中有按鈕,我的問題是如何將操作添加到按鈕iv添加到UIViews子視圖?謝謝,在子視圖中爲按鈕添加動作

回答

1

假設你在視圖控制器中爲你的設置編寫代碼UIView,UIView已被正確綁定到你的控制器的view屬性,並且你引用了一個帶有變量button的按鈕之一,這裏是你可以這樣寫:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [button addTarget:self 
       action:@selector(buttonPressed) 
    forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void)buttonPressed 
{ 
    // do things here in response to the button being pressed 
} 

另一種方式來編寫方法傳遞一個指針,它實際上按下按鈕,像這樣:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [button addTarget:self 
       action:@selector(buttonPressed:) // note the extra colon here 
    forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void)buttonPressed:(id)sender 
{ 
    UIButton *buttonWhichWasPressed = (UIButton *)sender; 
    // now you can do things like hide the button, change its text, etc. 
} 

不需要調用addTarget:action:forControlEvents:,日在界面生成器(中,你應該這樣做)定義buttonPressedbuttonPressed:方法之後,在.xib文件中點擊按鈕後按鈕進入檢查器中的第二個選項卡(應該說按鈕連接) ,然後單擊並將Touch Up Inside事件拖放到文件所有者,然後從列表中選擇「buttonPressed」。

+0

非常感謝你! – 2011-03-02 04:33:28

相關問題