在 MainWindow.xib 中显示带按钮的 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!
I'd like to display a ModalViewController from a bar button in the MainWindow.xib file. How would I do this? The basic code I'm looking to use is this:
-(IBAction)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
But where do I put it?
EDIT: I figured it out, in my navigation controller i put the following code in viewDidLoad:
UIBarButtonItem *addbutton = self.navigationItem.leftBarButtonItem;
[addbutton setTarget:self];
[addbutton setAction:@selector(add)];
and changed the function to:
- (void)add {
myCustomViewController *add = [[myCustomViewController alloc] initWithNibName:@"myCustomViewController" bundle:nil];
[self presentModalViewController:add animated:YES];
[add release];
}
Thanks for your help, Parth!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕这是不可能的。
您必须将 viewController 放入 MainWindow.xib 中,并将按钮放在该 viewController 上,因为您无法在 UIWindow 上添加控件(如您的情况下的按钮)。
它必须是 UIViewController 或 UITableViewController 类型才能向其添加 UIControl。
希望这对您有帮助。
I fear that this is not possible.
You will have to put a viewController inside MainWindow.xib and put button on that viewController because you cannot add controls (like button in your case) on UIWindow.
It is required to be of type UIViewController or UITableViewController for you to be able to add UIControls to it.
Hope this helps you.