S.J
S.J

Reputation: 3071

I am really confused regarding few things in UIViewController

I am really confused regarding few things in UIViewController, I have already read the View Controller Programming Guide and searched lot on the Internet but still confused.

When I want to jump or switch from firstVC to secondVC how many types of methods are available? I am listing which I know:

  1. UINavigationController

  2. UITabBarController

  3. presentModalViewController:

  4. Add secondVC to root view

    • If secondVC is added to root view then how firstVC object will be released?
    • Is it a good practice to add every view I want to jump/switch to root view?
  5. transitionFromView:

    • I dont understand this portion from Apple doc:

This method modifies the views in their view hierarchy only. It does not modify your application’s view controllers in any way. For example, if you use this method to change the root view displayed by a view controller, it is your responsibility to update the view controller appropriately to handle the change.

If I do this:

secondViewController *sVc = [[secondViewController alloc]init];

[transitionFromView:self.view toView:sVc.view...

Still viewDidLoad:, viewWillAppear:, viewDidAppear: are working fine: I don't need to call them. So why did Apple say this:

it is your responsibility to update the view controller appropriately to handle the change.

Are there any other methods available?

Upvotes: 1

Views: 106

Answers (1)

Anil
Anil

Reputation: 1028

Actually the standard methods used are :

1) Using NavigationController

 //push the another VC to the stack
[self.navigationController pushViewController:anotherVC animated:YES];

//remove it from the stack
[self.navigationController popViewControllerAnimated:NO];

//or presenting another VC from current navigationController     
[self.navigationController presentViewController:anotherVC animated:YES completion:nil];

//dismiss it
[self.navigationController dismissViewControllerAnimated:YES completion:nil];

2) Presenting the VC

//presenting another VC from current VC     
[self presentViewController:anotherVC animated:YES completion:nil

//dismiss it
[self dismissViewControllerAnimated:YES completion:nil];

Never use the method you described in points 4. It's not a good practice to change root view controller's dynamically. window's root VC is usually defined on applicationdidfinishlaunchingwithoptions after that it shouldn't be changed , if you are to follow apple standards.

Example for transitionFromView:toView

-(IBAction) anAction:(id) sender {
// assume view1 and view2 are some subviews of self.view
// view1 will be replaced with view2 in the view hierarchy
[UIView transitionFromView:view1 
                    toView:view2 
                  duration:0.5 
                   options:UIViewAnimationOptionTransitionFlipFromLeft   
                completion:^(BOOL finished){
                    /* do something on animation completion */
                  }];
  }

}

Upvotes: 1

Related Questions