2 mapmapm MapMapM 于 2013.07.29 13:58 提问

点击自定义的按钮应用就会中止

创建了导航条的自定义按钮,点击时,会中止:

-(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) 

1个回答

Mr_me
Mr_me   2013.07.29 15:40
已采纳

你的按钮是在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的选择器。

MapMapM
MapMapM 非常感谢~~
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!