【问题标题】:Application terminated when tapping custom button on navigation bar点击导航栏上的自定义按钮时应用程序终止
【发布时间】:2013-07-28 19:06:15
【问题描述】:

我为导航栏制作了一个自定义按钮,但是当我点击它时,它会终止

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

【问题讨论】:

  • 这不是您的问题/问题的一部分,但您应该尝试养成在做任何其他事情之前调用-super 方法(如-viewDidLoad)的习惯。

标签: iphone uibutton termination


【解决方案1】:

您的按钮正在尝试在您的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 的选择器不存在。当您的应用因这些错误而终止时,您应该检查所有 @selector() 语句是否与实际方法名称匹配。

【讨论】:

    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 2012-01-03
    • 2021-09-09
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多