1

我通過從蘋果直營店去「Your first iOS app的UIButton removeFromSuperview

,現在我有一個按鈕,這是不是在ViewController中宣稱:

@interface HelloWorldViewController : UIViewController <UITextFieldDelegate> 
- (IBAction)changeGreeting:(id)sender; 
@property (weak, nonatomic) IBOutlet UITextField *textField; 
@property (weak, nonatomic) IBOutlet UILabel *label; 
@property (copy, nonatomic) NSString *userName; 
@end 

現在我可以刪除標籤(和文本字段),使用[label removeFromSuperview];但我不明白如何用按鈕來做到這一點。有人可以幫忙嗎?

回答

1

您應該添加一個IBOutlet的按鈕,像你一樣的文本框和標籤:

@property (weak, nonatomic) IBOutlet UITextField *textField; 
@property (weak, nonatomic) IBOutlet UILabel *label; 
@property (weak, nonatomic) IBOutlet UIButton *button; // Don't forget to link to this from Interface Builder 
// ... 

然後你可以使用刪除的按鈕:

[button removeFromSuperview]; 

還要注意,你鏈接到的教程說:

動作方法中的發件人參數指的是發送操作消息(在本教程中,發件人是按鈕)的對象

所以,如果你想刪除時,它被竊聽(內changeGreeting:)按鈕,那麼你不需要IBOutlet,因爲你已經在sender參數按鈕的參考:

- (IBAction)changeGreeting:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    // ... 
    [button removeFromSuperview]; 
    // ... 
} 
0

您需要在控制器申報按鈕像你這樣爲IBAction,這個時候這個聲明爲一個出口(IBoutlet)..這樣你會得到它的代碼參考..

或者 ..你可以將代碼設置爲在Interface Builder ..按鈕

,然後使用viewWithTag在代碼檢索:方法