سؤال

I made an custom button for a navigation bar, but when I tap it, it terminates

-(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];
}

the output is

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) 
هل كانت مفيدة؟

المحلول

You button is trying use the selector back on your SleepModeViewController, but you've actually named the method -goBackOne. You fix it, either rename the -goBackOne method to -back, or change the name of the selector to goBackOne. For example:

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

It's important that the selector name and method names match. The error shows that your problem is that the selector named -back doesn't exist. When your app terminates with these errors, you should check that all your @selector() statements match actual method names.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top