我有一个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

请帮忙 。

最佳答案

您收到的错误消息表明,Score(您试图从中推送新ViewController的类)不是ViewController。
只有ViewController可以显示其他Viewcontroller。演示文稿的代码应放置在ViewController中:

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

关于ios - 通过UIButton切换 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10767376/

10-09 06:31