按UIBarButtonItem时无法识别的选择器发送到实例+崩溃

时间:2014-04-04 00:33:54

标签: ios objective-c

我得到NSException“无法识别的选择器发送到实例”,然后每当按下btnReload UIBarButtonItem时我的应用程序崩溃。我包含了该方法为操作执行的操作的代码,但无论testChangingViewController方法是否包含代码,都会发生崩溃/异常

以下所有代码都在我的UIViewController类中。

- (void)loadView //use loadView because not a storyboard based GUI.
{
    [super loadView];

    UIBarButtonItem *btnReload =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(testChangingViewController:)];
    self.navigationController.topViewController.navigationItem.leftBarButtonItem = btnReload;
    btnReload.enabled=TRUE;

    //self.view = [[UIView alloc] initWithFrame:[[UIScr een mainScreen] bounds]];
    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self setTitle:@"Navigation"]; //navigation bar title tied to the view controller it is managing.

    UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"change_user-512"]];
    [friendsIcon setFrame:CGRectMake(60, 130, 70, 70)];
    [self.view addSubview:friendsIcon];

    UIImageView *statusIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk-512"]];
    [statusIcon setFrame:CGRectMake(180,130, 70, 70)];
    [self.view addSubview:statusIcon];

    UIView *square = [[UIView alloc] initWithFrame:CGRectMake(250, 250, 50, 50)];
    [square setBackgroundColor:[UIColor orangeColor]];
    [self.view addSubview:square];
    // Do any additional setup after loading the view.
}



-(void)testChangingViewController
{
    UITableViewController *newViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    [self.navigationController pushViewController:newViewController animated:YES];
}

1 个答案:

答案 0 :(得分:2)

您将UIBarButtonItem分配给@selector(testChangingViewController :)(结尾处的冒号,一个参数),但是您定义了方法testChangingViewController(没有冒号,没有参数)。它正在寻找的选择器不存在,因此您的错误。这些目标选择器总是倾向于有一个参数," sender",即生成事件的UIControl。