switch between uiviews with buttons and not uinavigation controllers

后端 未结 2 1816
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  • 2020-12-30 17:46

    You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.

    You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.

    // origView is an instance variable/IBOutlet to your original view.
    - (IBAction)switchToPhoneView:(id)sender {
      if (origView == nil)
        origView = self.view;
      self.view = phoneViewController.view;
    }
    
    - (IBAction)switchToOriginalView:(id)sender {
      self.view = origView;
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题