2011-03-16 81 views
0

我想從MainWindow.xib文件中的bar按鈕顯示ModalViewController。我將如何做到這一點?我期待使用的基本代碼是這樣的:使用MainWindow.xib中的按鈕顯示ModalViewController

-(IBAction)add { 
    myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil]; 
    [self presentModalViewController:add animated:YES]; 
    [add release]; 
} 

但我在哪裏放?

編輯:我想通了,在我的導航控制器我把下面的代碼中viewDidLoad中:

UIBarButtonItem *addbutton = self.navigationItem.leftBarButtonItem; 
[addbutton setTarget:self]; 
[addbutton setAction:@selector(add)]; 

,改變了功能:

- (void)add { 
    myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil]; 
    [self presentModalViewController:add animated:YES]; 
    [add release]; 
} 

感謝您的幫助,PARTH!

回答

0

我擔心這是不可能的。

您必須在MainWindow.xib中放置一個viewController,並將該按鈕放在該viewController上,因爲您無法在UIWindow中添加控件(如您的案例中的按鈕)。

它需要是UIViewController或UITableViewController類型才能夠將UIControls添加到它。

希望這可以幫助你。

+0

它實際上是一個導航控制器上的Bar按鈕,它有什麼區別嗎?它在編譯時顯示在視圖上,我只是不知道如何在點擊時顯示modalviewcontroller。 – 2011-03-16 09:25:43

+0

@Simon:你是通過編碼還是通過XIB創建按鈕? – 2011-03-16 09:39:37

+0

你可以把你的方法'add'加入到你的AppDelegate.m文件中。 – 2011-03-16 09:45:22