使用MainWindow.xib中的按钮显示ModalViewController

时间:2011-03-16 09:04:43

标签: iphone modalviewcontroller mainwindow

我想从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!

1 个答案:

答案 0 :(得分:0)

我担心这是不可能的。

您必须将一个viewController放在MainWindow.xib中并将该按钮放在该viewController上,因为您无法在UIWindow上添加控件(如您的情况中的按钮)。

它必须是UIViewController或UITableViewController类型,才能为其添加UIControl。

希望这会对你有所帮助。