[英]App crash when UIBarButtonItem is pressed
My app crashes when I tap on an UIBarButtonItem.当我点击 UIBarButtonItem 时,我的应用程序崩溃。 I tried many different ways to write the function that will be called but could not find the solution so far.
我尝试了许多不同的方法来编写将被调用的函数,但到目前为止找不到解决方案。 My code is below.
我的代码如下。 Thank you for your help!
感谢您的帮助!
#import <Foundation/Foundation.h>
#include<pthread.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import <ContactsUI/CNContactViewController.h>
#import <UIKit/UIKit.h>
@interface AddressBookAccess:UIViewController
@end
@implementation AddressBookAccess:UIViewController
+(BOOL)AddNewContact
{
// Removing code here
newNavigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back to AReach" style:UIBarButtonItemStylePlain target:self action:@selector(pressButton:)];
[viewController presentViewController:newNavigationController animated:YES completion:nil];
return YES;
}
-(IBAction)pressButton:(UIBarButtonItem*)btn
{
NSLog(@"button tapped %@", btn.title);
}
@end
Dan.担。 I noticed that you added a button to the class method and bound the event, where
self
refers to the current class, then the button should bound to a class method.我注意到你在类方法中添加了一个按钮并绑定了事件,其中
self
指的是当前类,那么按钮应该绑定到一个类方法。
Now the button method you provide is an instance method, so you will get an error like this.现在你提供的按钮方法是一个实例方法,所以你会得到这样的错误。
+[AddressBookAccess pressButton:]: unrecognized selector sent to class 0x104d2dac8
Since you noticed that your event is associated with the storyboard, I suggest you create a button in the instance method and bind the event.由于您注意到您的事件与故事板相关联,我建议您在实例方法中创建一个按钮并绑定事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.