switch between uiviews with buttons and not uinavigation controllers

后端 未结 2 1817
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 17:26

I have seen the post for How to switch views by buttons on iPhone? but this doesn\'t answer how to switch back and forth between views with buttons. The person that asked t

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 18:04

    The technique I usually use involves creating a superview class which contains a toolbar at the bottom, and a content view UIView class filling the rest of the screen.

    I then add subviews to the content view to change the views based on button clicks. This approach makes it so the toolbar on the same is constant across all views. I start by defining a helper function like this:

    -(void) clearContentView {
        //helper to clear content view
        for (UIView *view in [self.contentView subviews]){
            [view removeFromSuperview];
        }
    }
    

    My IBAction then looks like this:

    -(IBAction) buttonClicked{
        self.title = @"Images"; //change title of view
        [self clearContentView]; //clear content view
        [self.contentView addSubview:self.imagesViewController.view]; //add new view
        [self.imagesViewController viewWillAppear:YES]; //make sure new view is updated
        [self enableButtons];  //enable all other buttons on toolbar
        self.imagesButton.enabled = NO;  //disable currently selected button
    }
    

提交回复
热议问题