0

我的代码中有一个错误,我似乎找不到它!我以编程方式创建了一个名为 addButton 的按钮,并将其选择器设置为 add,但每次按下该按钮时,应用程序都会在模拟器中崩溃。这是我的代码。

-(void)viewDidLoad{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]  
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self   
action:@selector(add:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}

添加按钮的代码是:

- (IBAction)add
{
MyDetailViewController * detail = [[MyDetailViewController alloc]init];
detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:detail animated:YES];
//[detail.text becomeFirstResponder];

[detail release];

}

感谢您的帮助:D

4

1 回答 1

2

您的 add 选择器末尾有一个冒号,这意味着它正在尝试使用带有参数的 add 方法,但您的 add 方法不需要参数对象。您需要通过将 Bar Button Item 分配更改为以下内容来从选择器中删除冒号:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
于 2011-08-20T01:00:02.193 回答