通过UIButton切换视图?

时间:2012-05-26 15:12:23

标签: ios uibutton

我有一个Xcode项目,想要从一个文件切换视图,文件名是Score(没有xib,只有Score.h和Score.m)到主文件(它的名字是View并且有一个XIB )在我的文件Score.m中使用UIbutton,但我不能这样做..

使用此代码:

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(200, 400, img3.size.width/2, img3.size.height/2);
[btn1 setImage:img3 forState:UIControlStateNormal];
[btn1 setImage:img4 forState:UIControlStateDisabled];
[self addSubview:btn1];

if(btn1.selected) {
    View *second =[[View alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:second animated:YES];
}

我收到此错误:Receiver type 'Score' for instance message doesn't declare a method with selector 'presentModalViewController : animated

请帮忙。

1 个答案:

答案 0 :(得分:0)

您收到的错误消息表明,Score(您尝试推送新ViewController的类)不是ViewController。 只有ViewControllers可以呈现其他Viewcontrollers。演示文稿的代码应该(放在ViewController中):

if(btn1.selected) {
    UIViewController *second =[[UIViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:second animated:YES];
    [second release]
}