2012-05-26 54 views
1

我有一个Xcode项目,并希望从一个文件切换视图,文件名是Score(没有xib,它只有Score.h和Score.m)到主文件(它的名称是View并且确实有XIB)在我的文件Score.m使用的UIButton,但我不能这样做..通过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

请帮助。

+0

你的分数是多少?你能告诉我们score.h吗? –

+0

什么是包含按钮的视图控制器?您可以从该视图控制器调用presentModalViewController。 – nhahtdh

+0

你说过View有一个XIB,所以你应该在initWithNibName中使用它。 'View * second = [[View alloc] initWithNibName:@“View.xib”bundle:nil];'。 – lnafziger

回答

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] 
} 
+0

你是对的这是问题,但是当我把这个方法没有发生,当我按下按钮..也在score.h中我把这个:[btn1 addTarget:self action: @selector(btn1.selected)forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn1]; – Hussain1982

+0

你说得对。我将通过尝试在相同的类之间创建方法来解决问题。 – Hussain1982