Sunday, June 9, 2013

UINavigationController popViewControllerAnimated example in Objective C (iOS).


UINavigationController popViewControllerAnimated

Pops the top view controller from the navigation stack and updates the display.

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

Parameters
animated
Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value of [UINavigationController popViewControllerAnimated]
The view controller that was popped from the stack.

Discussion of [UINavigationController popViewControllerAnimated]
This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack.[UINavigationController popViewControllerAnimated]

In addition to displaying the view associated with the new view controller at the top of the stack, this method also updates the navigation bar and tool bar accordingly. In iOS 3.0 and later, the contents of the built-in navigation toolbar are updated to reflect the toolbar items of the new view controller. For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

UINavigationController popViewControllerAnimated example.
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;

// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];

// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];
[navController pushViewController:someViewController animated:NO];

Example of [UINavigationController popViewControllerAnimated].
MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];

    UINavigationController *navController = self.navigationController;     
    [[self retain] autorelease];

    [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration: 0.7];
    [UIView setAnimationTransition:<#UIViewAnimationTransitionCurlDown#> forView:navController.view cache:NO];

    [navController popViewControllerAnimated:NO];
    [navController pushViewController:mevc animated:NO];

    [UIView commitAnimations];

UINavigationController popViewControllerAnimated example.
    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];

End of UINavigationController popViewControllerAnimated example article.

UINavigationController popToViewController animated example in Objective C (iOS).


UINavigationController popToViewController animated

Pops view controllers until the specified view controller is at the top of the navigation stack.

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

Parameters of [UINavigationController popToViewController animated]
viewController
The view controller that you want to be at the top of the stack.
animated
Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value
An array containing the view controllers that were popped from the stack.

Discussion of [UINavigationController popToViewController animated]
For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

UINavigationController popToViewController animated example.
- (void) popControllersNumber:(int)number
{
    if (number <= 1)
        [[self navigationController] popViewControllerAnimated:YES];
    else
    {
        NSArray* controller = [[self navigationController] viewControllers];
        int requiredIndex = [controller count] - number - 1;
        if (requiredIndex < 0) requiredIndex = 0;
        UIViewController* requireController = [[[self navigationController] viewControllers] objectAtIndex:requiredIndex];
        [[self navigationController] popToViewController:requireController animated:YES];
    }
}

Example of [UINavigationController popToViewController animated].
TasksViewController *taskViewController = [[TasksViewController alloc] initWithNibName:nil bundle:nil];

if ([navigationController.viewControllers indexOfObject:taskViewController] == NSNotFound)
{
    [navigationController pushViewController:taskViewController animated:animated];
}
else
{
    [navigationController popToViewController:taskViewController animated:animated];
}

UINavigationController popToViewController animated example.
YourViewController *yourViewController;
for ( UIViewController *viewController in self.navigationController.viewControllers ) {
    if ( [viewController isMemberOfClass:[YourViewController class]] ) {
        yourViewController = (YourViewController*)viewController;
        break;
    }
}

[self popToViewController:yourViewController animated:YES];

End of UINavigationController popToViewController animated example article.

UINavigationController popToViewController example in Objective C (iOS).


UINavigationController popToViewController

Pops view controllers until the specified view controller is at the top of the navigation stack.

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

Parameters of [UINavigationController popToViewController]
viewController
The view controller that you want to be at the top of the stack.
animated
Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value
An array containing the view controllers that were popped from the stack.

Discussion of [UINavigationController popToViewController]
For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

UINavigationController popToViewController example.
- (void) popControllersNumber:(int)number
{
    if (number <= 1)
        [[self navigationController] popViewControllerAnimated:YES];
    else
    {
        NSArray* controller = [[self navigationController] viewControllers];
        int requiredIndex = [controller count] - number - 1;
        if (requiredIndex < 0) requiredIndex = 0;
        UIViewController* requireController = [[[self navigationController] viewControllers] objectAtIndex:requiredIndex];
        [[self navigationController] popToViewController:requireController animated:YES];
    }
}

Example of [UINavigationController popToViewController].
TasksViewController *taskViewController = [[TasksViewController alloc] initWithNibName:nil bundle:nil];

if ([navigationController.viewControllers indexOfObject:taskViewController] == NSNotFound)
{
    [navigationController pushViewController:taskViewController animated:animated];
}
else
{
    [navigationController popToViewController:taskViewController animated:animated];
}

UINavigationController popToViewController example.
YourViewController *yourViewController;
for ( UIViewController *viewController in self.navigationController.viewControllers ) {
    if ( [viewController isMemberOfClass:[YourViewController class]] ) {
        yourViewController = (YourViewController*)viewController;
        break;
    }
}

[self popToViewController:yourViewController animated:YES];

End of UINavigationController popToViewController example article.

UINavigationController popToRootViewControllerAnimated example in Objective C (iOS).


UINavigationController popToRootViewControllerAnimated

Pops all the view controllers on the stack except the root view controller and updates the display.

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

Parameters
animated
Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value
An array of view controllers that are popped from the stack.

Discussion of [UINavigationController popToRootViewControllerAnimated]
The root view controller becomes the top view controller. For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

UINavigationController popToRootViewControllerAnimated example.
Calling [self.navigationController popToRootViewControllerAnimated:YES] sets self.navigationController to nil. When you subsequently call [self.navigationController pushViewController:someOtherViewController] you are effectively sending a message to nil, which does nothing.

To workaround, simply set up a local reference to the navigationController and use that instead:

UINavigationController * navigationController = self.navigationController;
[navigationController popToRootViewControllerAnimated:NO];
[navigationController pushViewController:someOtherViewController animated:YES];

Example of [UINavigationController popToRootViewControllerAnimated].
[self dismissModalViewControllerAnimated:YES];
[self performSelector:@selector(patchSelector) withObject:nil afterDelay:0.3];

-(void)patchSelector{
  [self.navigationController popToRootViewControllerAnimated:YES];
}

UINavigationController popToRootViewControllerAnimated example.
You can return to the first view with

[self.navigationController popToRootViewControllerAnimated:YES];
That being said, you can also remove a particular view controller, or navigate to a specific index in your view controller if you look at the example.

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack  

[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this.

navigationController.viewControllers = allViewControllers;

End of UINavigationController popToRootViewControllerAnimated example article.

UINavigationController initWithRootViewController example in Objective C (iOS).


UINavigationController initWithRootViewController

Initializes and returns a newly created navigation controller.

- (id)initWithRootViewController:(UIViewController *)rootViewController

Parameters of [UINavigationController initWithRootViewController]
rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.

Return Value
The initialized navigation controller object or nil if there was a problem initializing the object.

Discussion of [UINavigationController initWithRootViewController]
This is a convenience method for initializing the receiver and pushing a root view controller onto the navigation stack. Every navigation stack must have at least one view controller to act as the root.

UINavigationController initWithRootViewController example.
incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
    incidentTableViewController.title = @"Incidents";
    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects:controller, nil];
    splitViewController.delegate = (id)incidentTableViewController;
    splitViewController.view.frame = CGRectMake(268, 0, 268, 423);
    [self.view addSubview:splitViewController.view];

Example of [UINavigationController initWithRootViewController].
- (void)viewDidLoad {
    [super viewDidLoad];

    PageOneController *one = [[[PageOneController alloc]init] autorelease];
    one.title = @"blah";
    navController = [[UINavigationController alloc] initWithRootViewController:one];
    [self.view addSubview:navController.view];
}

UINavigationController initWithRootViewController example.
- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *one = [[[UIViewController alloc] init] autorelease];

    [one.view setBackgroundColor:[UIColor yellowColor]];
    [one setTitle:@"One"];

    navController = [[UINavigationController alloc] initWithRootViewController:one];
    // here 's the key to the whole thing: we're adding the navController's view to the
    // self.view, NOT the one.view! So one would be the home page of the app (or something)
    [self.view addSubview:navController.view];

    one = [[[UIViewController alloc] init] autorelease];

    [one.view setBackgroundColor:[UIColor blueColor]];
    [one setTitle:@"Two"];

    // subsequent views get pushed, pulled, prodded, etc.
    [navController pushViewController:one animated:YES];
}

End of UINavigationController initWithRootViewController example article.

UINavigationController initWithNavigationBarClass toolbarClass example in Objective C (iOS).


UINavigationController initWithNavigationBarClass toolbarClass

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass

Parameters of [UINavigationController initWithNavigationBarClass toolbarClass]
navigationBarClass
Specify the custom UINavigationBar subclass you want to use, or specify nil to use the standard UINavigationBar class.
toolbarClass
Specify the custom UIToolbar subclass you want to use, or specify nil to use the standard UIToolbar class.

Return Value of [UINavigationController initWithNavigationBarClass toolbarClass]
The initialized navigation controller object or nil if there was a problem initializing the object.

Discussion of [UINavigationController initWithNavigationBarClass toolbarClass]
Use this initialization method if you want to use custom navigation bar or toolbar subclasses with the navigation controller. If you use this method, you are responsible for adding a root view controller before presenting the navigation controller onscreen.

UINavigationController initWithNavigationBarClass toolbarClass example.
// This code assumes `MyCustomNavigationBar` is the name of your custom subclass, and that `viewController` is a UIViewController object created earlier.

// To create the containing navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];

// To set the root view controller in the navigation controller
navigationController.viewControllers = @[viewController];

Example of [UINavigationController initWithNavigationBarClass toolbarClass].
Like this:

UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];

[navigationController setViewControllers:@[yourRootViewController] animated:NO];

UINavigationController initWithNavigationBarClass toolbarClass example.
e.g.

@interface MyBar : UINavigationBar
@end

@implementation MyBar
.... //like any UIView
@end
UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[MyBar class] toolbarClass:nil];
instead of initWithRootViewController

Sample

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPhone" bundle:nil];
} else {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPad" bundle:nil];
}

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
navi.viewControllers = @[self.mainViewController];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}

End of UINavigationController initWithNavigationBarClass toolbarClass example article.

UINavigationController initWithNavigationBarClass example in Objective C (iOS).


UINavigationController initWithNavigationBarClass

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass

Parameters
navigationBarClass
Specify the custom UINavigationBar subclass you want to use, or specify nil to use the standard UINavigationBar class.
toolbarClass
Specify the custom UIToolbar subclass you want to use, or specify nil to use the standard UIToolbar class.

Return Value of [UINavigationController initWithNavigationBarClass]
The initialized navigation controller object or nil if there was a problem initializing the object.

Discussion of [UINavigationController initWithNavigationBarClass]
Use this initialization method if you want to use custom navigation bar or toolbar subclasses with the navigation controller. If you use this method, you are responsible for adding a root view controller before presenting the navigation controller onscreen.

UINavigationController initWithNavigationBarClass example.
// This code assumes `MyCustomNavigationBar` is the name of your custom subclass, and that `viewController` is a UIViewController object created earlier.

// To create the containing navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];

// To set the root view controller in the navigation controller
navigationController.viewControllers = @[viewController];

Example of [UINavigationController initWithNavigationBarClass].
Like this:

UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];

[navigationController setViewControllers:@[yourRootViewController] animated:NO];

UINavigationController initWithNavigationBarClass example.
e.g.

@interface MyBar : UINavigationBar
@end

@implementation MyBar
.... //like any UIView
@end
UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[MyBar class] toolbarClass:nil];
instead of initWithRootViewController

Sample

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPhone" bundle:nil];
} else {
    self.mainViewController = [[FDMainViewController alloc] initWithNibName:@"FDMainViewController_iPad" bundle:nil];
}

UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[UINavigationBar class] toolbarClass:nil];
navi.viewControllers = @[self.mainViewController];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}

End of UINavigationController initWithNavigationBarClass example article.