将UIViewController视图嵌入另一个UIViewController中
在我的应用中,我有一个 login-register-forgotpasword 线框.该过程的每个步骤都在单独的 UIViewController
中实现,它们都在同一个 Storyboard
中,并且每个控制器之间的过渡都是动态的.到目前为止一切顺利.
In my app, I have a login-register-forgotpasword wireframe. Each step of the process is implemented in a separate UIViewController
, all of them inside the same Storyboard
, and transitions between each controller are animated. So far so good.
现在我们已经更改了设计,因此所有视图都具有相同的背景元素和标题(不完全是 UINavigationBar
),并且我不喜欢视图的动画效果看起来总是一样,只是表现出不同的形式.因此,我正在考虑采用不同的方法,而不是推送整个控制器,而只是显示/隐藏其视图,而是保留在同一控制器中.-
Now we've changed the design, so all views have the same background elements and a header (not exactly a UINavigationBar
), and I don't like the feel of the animation to a view that always looks to be actually the same, but just showing a different form. So I'm considering different approaches to, instead of pushing whole controllers, just showing/hiding its views, but staying in the same controller.-
1)我的第一次尝试是实例化控制器要显示的视图,并将其添加到当前视图.像这样.-
1) My first try was instantiating the controller which view I want to show, and add it to the current view. Something like.-
- (IBAction)btnRegisterPressed:(id)sender {
_viewHome.hidden = YES;
RegisterController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"registerNewUser"];
[self.view addSubview:controller.view];
}
这是完美的,因为我为表单使用静态 UITableViews
,据我所知,每个表单都需要一个 UITableViewController
.但是似乎 IBOutlets
和 IBActions
坏了.以某种方式可以实现这种方法吗?如果是这样,这被认为是不好的做法吗?
This one would be perfect, as I'm using static UITableViews
for my forms, so as far as I know I'd need a UITableViewController
for each one. But it seems IBOutlets
and IBActions
got broken. Is this approach possible in some way? If so, it's considered a bad practice?
2)我的第二个选择是在一个控制器中创建所有视图,并正确显示/隐藏它们.这将很难维护,而且我不得不忘记静态的 UITableViews
.
2) My second option is just creating all the views inside one controller, and properly showing/hiding them. This would one be hard to maintain, and chances are I'd have to forget about static UITableViews
.
有人能给我一些建议哪种选择更好的建议,或者向我指出我所缺少的任何其他可能的方法吗?
您的选项#1不适合编写,但接近您可能应该考虑的方法.您可以添加嵌套不同 UIViewController
的视图,但是这样做时,您应该使用
Your option #1 is not appropriate as written but close to an approach you should probably consider. You can add nest the views of different UIViewController
s however when you do so you should use the methods described in Managing Child View Controllers in a Custom Container so that the parent controller correctly manages its child controllers.