2011-03-13 32 views
0

我的錯誤是:添加的UIButton到的UITextField導致無法識別的選擇發送

2011-03-12 20:48:33.861 SmarTrek[22040:207] -[RouteViewController selectFavorite]: unrecognized selector sent to instance 0x5b154a0 
2011-03-12 20:48:33.863 SmarTrek[22040:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException' 

的代碼是:

- (void)viewDidLoad 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal]; 
    [button setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateHighlighted]; 
    button.bounds = CGRectMake(0, 0, 0, 29); 
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -24, 0, 0); 
    [button addTarget:self action:@selector(selectFavorite) forControlEvents:UIControlEventTouchUpInside]; 


    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button1 setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal]; 
    [button1 setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateHighlighted]; 
    button1.bounds = CGRectMake(0, 0, 0, 29); 
    button1.imageEdgeInsets = UIEdgeInsetsMake(0, -24, 0, 0); 
    [button1 addTarget:self action:@selector(selectFavorite) forControlEvents:UIControlEventTouchUpInside]; 

    origin.rightView = button; 
    destination.rightView = button1; 
    origin.rightViewMode = UITextFieldViewModeAlways; 
    destination.rightViewMode = UITextFieldViewModeAlways; 


    self.navigationItem.title = @"Where to go?"; 
    //self.navigationItem.leftBarButtonItem; 
    //self.navigationItem.rightBarButtonItem; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (IBAction) selectFavorite:(id) sender 
{ 
    NSLog(@"TEST"); 

} 

回答

1

通過@selector(selectFavorite)@selector(selectFavorite:)提到的方法是不一樣的,在結腸意義重大。由於您的方法是- (IBAction) selectFavorite:(id) sender,因此您需要在@selector中包含冒號。

+0

錯過了!感謝指出這一點..順便說一句,我不能只有一個UIButton在兩個地方使用? – aherlambang

+0

如果兩個不同的地方同時不存在,即使從一個視圖控制器轉換到另一個視圖控制器,您也可能會擺脫它。否則,你會遇到麻煩的地方,因爲它已被用於其他地方,該按鈕從哪裏丟失。最好是創建兩個相同的按鈕實例並避免所有的麻煩。 – Anomie

+0

gotcha!謝謝一堆 – aherlambang

相關問題