我的脚被objective-C弄湿了。我想我应该玩玩iphone的开发。我有一个页面控制器.h和.m
在我的实现文件中,我在viewDidLoad方法中抛出了一个UIButton。
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:@"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
// add to a view
[self.view addSubview:myButton];
}到目前为止,它显示得很好。这让我不禁要问loadView和viewDidLoad之间的区别。
另外,我想在按钮被按下时运行我自己的函数,但是我经常从xcode得到一个可执行的错误。
下面是我在上面的代码中插入的内容:
- (void)viewDidLoad
{
[super viewDidLoad];
// content that loads in view
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(10, 160, 300, 44);
[myButton setTitle:@"Press Me!" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:14];
//added this
[myButton addTarget:self action:@selector(runClick) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:myButton];
}
-(void)runClick{
NSLog(@"does this work?");
}有没有人觉得不对劲?
另外,在PageController.h中我添加了:-(空)runClick;
下面是我捕捉到的错误:
2012-03-17 16:04:30.077 Blah510:30.077 -__NSCFString runClick:无法识别的选择器发送到实例0x6a0b4f0
发布于 2012-03-18 06:03:17
关于loadView和viewDidLoad之间的区别:
根据苹果公司的说法,
上删除它
关于这个错误,你能提供更多的信息吗?你在调试吗?它崩溃的线路是什么?
发布于 2012-03-18 06:29:06
您是否在.h文件中声明了-(void)runClick?(显而易见的问题)。
另外,如果你想访问控件的属性(假设它是一个滑块,等等),你需要添加发送器位:
-(void)runClick (id)sender
发布于 2012-07-06 19:32:44
我认为您需要在runClick后面添加一个冒号,如下所示:
action:@selector(runClick:) https://stackoverflow.com/questions/9754098
复制相似问题