2012-05-26 53 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