如何调试 iPhone 应用程序?我怎样才能知道模拟器中发生了什么?我是Xcode开发的初学者,不知道下面的代码有什么问题。单击按钮时应用程序崩溃。
- (void)viewDidLoad {
myLabel = [[UILabel alloc]init];
[myLabel setText:@"Labela"];
myLabel.frame = CGRectMake(50.0,50.0, 100.0, 30.0);
[self.view addSubview:myLabel];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Klikni" forState:UIControlStateNormal];
[myButton setFrame:CGRectMake(80.0, 210.0, 160.0, 40.0)];
[self.view addSubview:myButton];
[super viewDidLoad];
}
- (void)buttonAction {
[myLabel setText:@"Promijenjen!"];
}
最佳答案
action:@selector(buttonAction:)
在这里您指定 buttonAction 选择器获取 1 个参数,但它被声明为没有:
- (void)buttonAction{
...
所以当按钮点击系统试图调用未定义的方法并导致崩溃。要解决这个问题,您应该将选择器名称更改为
action:@selector(buttonAction)
或更改操作方法声明:
- (void)buttonAction:(id)sender{
...
关于iphone - 调试 iPhone 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3821853/