好吧,我正在使用我发现的UiWebView示例制作应用程序。我需要添加一个按钮,但这个例子的工作方式类似于phonegap所以我想出了我需要将按钮作为子视图添加到窗口,所以然后我得到了我的按钮并设置它按下但是当我按它我的应用程序崩溃...可以任何人的帮助吗?这是我的代码片段:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
myLoadingLabel.hidden = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[window addSubview:webView];
//my button
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
playButton.frame = CGRectMake(122, 394, 76, 76);
[playButton setTitle:@"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"mic.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"mic.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:playButton];
}
-(void)playAction {
NSLog(@"Button Pressed!");
}
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIWebView_ExampleAppDelegate playAction:]:
答案 0 :(得分:2)
您正在调用名为playAction:
的方法,但实现了名为playAction
的方法。注意缺少的结肠。崩溃最有可能与该问题有关。
要快速修复,请更改
[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
到
[playButton addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];
答案 1 :(得分:1)
问题在于:
@selector(playAction:)
VS
- (void)playAction
前者指的是一个名为-playAction:
的方法,它接受一个参数,但是您实现了一个名为-playAction
的方法,该方法不带参数。从@selector
块中删除冒号,它应该可以工作。