Embedding UIViewController view inside another UIV

2019-07-30 00:45发布

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.

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) 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];
}

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) 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.

Could anyone give me some advice of which option would be better, or point me to any other possible approach I'm missing?

3条回答
来,给爷笑一个
2楼-- · 2019-07-30 01:11

You can use as many tableviews as you want for the same controller. The delegate methods contains the object that fired the method itself.

Following this approach (which i wouldn't recommend) you can probably tag your tableviews and then do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (tableView.tag == 1) {
        doStuff;
    } else if (tableView.tag == 2) {
        doOtherStuff;
    }
}
查看更多
走好不送
3楼-- · 2019-07-30 01:12

Well, personally, I feel we should keep things simple and straight forward for end user to quickly gel up with your app. Animations plays an important role in this. Tapping on cell of a tableview pushes another table view and there are different UI widgets like chevron that indicates the transition and user is not surprised.

Showing everything on single screen and hiding/unhiding them based on user's action is fine as long as you have proper animation. For instance, you might have seen TableView sections collapsing/expanding on tap.

You need to make a trade off and see what best suits based on your application. Whatever you decide my suggestion would be to add nice animations instead of simple hide/unhide.

查看更多
【Aperson】
4楼-- · 2019-07-30 01:17

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 UIViewControllers 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.

查看更多
登录 后发表回答