【问题标题】:App crash when UIBarButtonItem is pressed按下 UIBarButtonItem 时应用程序崩溃
【发布时间】:2019-07-19 11:30:17
【问题描述】:

当我点击 UIBarButtonItem 时,我的应用程序崩溃了。我尝试了许多不同的方法来编写将被调用的函数,但到目前为止找不到解决方案。我的代码如下。感谢您的帮助!

#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

【问题讨论】:

  • 请添加有关崩溃的更多信息。

标签: ios objective-c button uibarbuttonitem


【解决方案1】:

丹。我注意到您在类方法中添加了一个按钮并绑定了事件,其中self 指的是当前类,那么按钮应该绑定到类方法。

现在你提供的按钮方法是一个实例方法,所以你会得到这样的错误。 +[AddressBookAccess pressButton:]: unrecognized selector sent to class 0x104d2dac8

由于您注意到您的事件与情节提要相关联,我建议您在实例方法中创建一个按钮并绑定事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多