Objective-C / Cocoa:正确加载UIViews

时间:2022-09-07 20:08:37

Suppose I have created a UIView, MyView, in Interface Builder, and I have hooked it up (set its File's Owner) to my UIViewController class, MyViewController.

假设我在Interface Builder中创建了一个UIView,MyView,我已将它(将其文件所有者设置)连接到我的UIViewController类MyViewController。

Now, I would like to present view. It's just another view, so I don't want to present it as a modal view.

现在,我想提出看法。这只是另一种观点,因此我不想将其作为模态视图呈现。

How do I go about displaying it? Should I add it as a subview of my window? If so, where does it go relative to my other views? Should I present it as a view in its own right somehow, and disable the other views? What is the mechanism?

我该如何展示它?我应该将其添加为窗口的子视图吗?如果是这样,相对于我的其他观点,它在哪里?我应该以某种方式将其作为一个视图以自己的方式呈现,并禁用其他视图?机制是什么?

2 个解决方案

#1


It depends on how you want the app to act.

这取决于您希望应用程序如何操作。

You can either add MyView as a subview of the current view using UIView addSubview if you are going to have a "Done" button or something like that on MyView to remove itself.

您可以使用UIView addSubview将MyView添加为当前视图的子视图,如果您要在MyView上使用“完成”按钮或其他类似按钮来删除自身。

// show new view
MyViewController *myViewController = [[MyViewController alloc]init];
[self.view addSubview: myViewController.view];

Or if you want the user to be able to navigate back to the main view(like in mail,notes etc) the most common way to do that would be to add a navigationController to your window and using pushViewController:animated: to present your views.

或者,如果您希望用户能够导航回主视图(如邮件,笔记等),最常见的方法是将navigationController添加到窗口并使用pushViewController:animated:来呈现您的视图。

MyViewController *myViewController = [[MyViewController alloc]init];
[self.navigationController pushViewController:myViewController animated:YES];

I much prefer the navigationController approach in most situations.

在大多数情况下,我更喜欢navigationController方法。

#1


It depends on how you want the app to act.

这取决于您希望应用程序如何操作。

You can either add MyView as a subview of the current view using UIView addSubview if you are going to have a "Done" button or something like that on MyView to remove itself.

您可以使用UIView addSubview将MyView添加为当前视图的子视图,如果您要在MyView上使用“完成”按钮或其他类似按钮来删除自身。

// show new view
MyViewController *myViewController = [[MyViewController alloc]init];
[self.view addSubview: myViewController.view];

Or if you want the user to be able to navigate back to the main view(like in mail,notes etc) the most common way to do that would be to add a navigationController to your window and using pushViewController:animated: to present your views.

或者,如果您希望用户能够导航回主视图(如邮件,笔记等),最常见的方法是将navigationController添加到窗口并使用pushViewController:animated:来呈现您的视图。

MyViewController *myViewController = [[MyViewController alloc]init];
[self.navigationController pushViewController:myViewController animated:YES];

I much prefer the navigationController approach in most situations.

在大多数情况下,我更喜欢navigationController方法。

#2