【发布时间】:2012-03-17 21:54:04
【问题描述】:
我被 Objective-C 弄湿了。并认为我会玩 iphone 开发。 我有一个 Page Controller.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 中我添加了:-(void)runClick;
这是我发现的错误:
2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]:无法识别的选择器发送到实例 0x6a0b4f0
【问题讨论】:
-
什么是可执行错误?
-
this: 2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]: 无法识别的选择器发送到实例 0x6a0b4f0
标签: xcode uibutton viewdidload