Switch between uiviews using buttons, not uinavigation controllers

I saw a message for How to switch button views on iPhone? but it doesn’t answer how to switch between views using buttons. The person who asked the question decided to answer that they can switch between views using the uinavigationcontroller.

I put the following code in ibaction, which starts when the button is clicked in the main view.

         PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:@"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];

When this code executes the whole view, it is simply empty. If I omit the removefromsuperview part, then the view disappears behind my button, but the button still remains. I'm not sure if this is the right way to switch between the buttons, but if anyone knows how to do this, please help. Also, if anyone knows of any sample projects that switch between views using buttons, let me know.

Thank you, million!

+3
source share
2 answers

You removed the view manager view from it, and then added a subroutine to it. The view hierarchy is broken down into the supervisor of the controller (probably your window). That is why you get a blank screen.

, , , .

// 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;
}
+2

, , , , UIView, .

subviews , . , . :

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

My IBAction :

-(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
}
+1

All Articles