2012-12-29 17 views
1

希望有人能幫助我,爲什麼我應該添加一個:調用該方法。爲什麼發送addTarget方法有:工作

這裏是viewDidLoad方法

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.view.backgroundColor= [UIColor yellowColor]; 
//Add Button 
CGRect buttonRect= CGRectMake(100, 100, 100, 44); 
UIButton *clickButton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[clickButton setFrame: buttonRect]; 
[clickButton setTitle:@"Click Me" forState: UIControlStateNormal]; 
[clickButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:clickButton]; 
[self buttonPressed:clickButton]; 
//Add UILabel 
CGRect labelViewRect= CGRectMake(50, 30, 200, 44); 
UILabel *labelview= [[UILabel alloc]initWithFrame:labelViewRect]; 
[labelview setText:@"Hello welcome to my app!"]; 
//Clear the UIView button's background 
[labelview setBackgroundColor:[UIColor clearColor]]; 
[self.view addSubview:labelview]; 
} 

這裏是按鈕按下方法

-(void)buttonPressed:(UIButton *)sender{ 
NSLog(@"This button was pressed"); 
self.view.alpha=((double)arc4random()/0x100000000); 
} 

當我刪除了:從

[clickButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

即我改變這上面一行的東西像這樣

[clickButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 

,則拋出異常:

2012年12月29日17:56:18.209 AlphaSchoolColor [11414:C07] - [com_skminfotekViewController buttonPressed]:無法識別的選擇發送到實例0xde2f050 2012年12月29日17:56:18.210 AlphaSchoolColor [11414:C07] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [com_skminfotekViewController buttonPressed]:無法識別的選擇發送到實例0xde2f050'

問題communi ty就是這樣發生的事情:我可以從哪裏得知更多信息,瞭解它的重要性。

謝謝。

回答

3

你的方法有一個參數。如果你想發送沒有「:」的動作,那麼改變你的方法。

-(void)buttonPressed{ 
} 
相關問題