在 MainWindow.xib 中显示带按钮的 ModalViewController

发布于 2024-10-22 11:26:53 字数 829 浏览 7 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-10-29 11:26:53

恐怕这是不可能的。

您必须将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文