程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> uibutton-點擊自定義的按鈕應用就會中止

uibutton-點擊自定義的按鈕應用就會中止

編輯:編程綜合問答
點擊自定義的按鈕應用就會中止

創建了導航條的自定義按鈕,點擊時,會中止:

-(void)viewDidLoad
{
  UIImage *backButtonImage = [UIImage imageNamed:@"button.png"];
  UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [backButton setImage:backButtonImage forState:UIControlStateNormal];
  backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
  [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
  UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
  self.navigationItem.leftBarButtonItem = customBackBarItem;
}
 -(void)goBackOne
{
  [self.navigationController popToRootViewControllerAnimated:YES];
}

輸出:

2013-07-28 15:00:37.932 Habit Pal[1562:c07] -[SleepModeViewController back]: unrecognized selector sent to instance 0x9167300
2013-07-28 15:00:37.932 Habit Pal[1562:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SleepModeViewController back]: unrecognized selector sent to instance 0x9167300'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x10e4705 0x182c0 0x18258 0xd9021 0xd957f 0xd86e8 0x47cef 0x47f02 0x25d4a 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x213d 0x2065)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

最佳回答:


你的按鈕是在SleepModeViewController試圖使用選擇器back,而實際上你給方法命名為-goBackOne。你應該重命名-goBackOne方法為-back,或者將選擇器的名字改為goBackOne。舉例:

// The selector must actually match a method name on the target
[backButton addTarget:self action:@selector(goBackOne) forControlEvents:UIControlEventTouchUpInside];

選擇器和方法命名匹配非常重要。錯誤是提示你沒有名為-back的選擇器。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved