1 Preface
UINavigationController enables the App to switch from an attempt controller to another one. It is very common in development. Today, let's take a look at this control.
2 UINavigation Introduction
Delegate code
. H file:
[Plain]
# Import <UIKit/UIKit. h>
# Import "ZYRootViewController. h"
@ Interface ZYAppDelegate: UIResponder <UIApplicationDelegate>
@ Property (strong, nonatomic) UIWindow * window;
@ Property (nonatomic, strong) ZYRootViewController * zYRootViewController;
@ Property (nonatomic, strong) UINavigationController * navigationController;
@ End
# Import <UIKit/UIKit. h>
# Import "ZYRootViewController. h"
@ Interface ZYAppDelegate: UIResponder <UIApplicationDelegate>
@ Property (strong, nonatomic) UIWindow * window;
@ Property (nonatomic, strong) ZYRootViewController * zYRootViewController;
@ Property (nonatomic, strong) UINavigationController * navigationController;
@ End. m file:
[Plain]
@ Synthesize navigationController;
@ Synthesize zYRootViewController;
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];
[Self. window makeKeyAndVisible];
Self. zYRootViewController = [[ZYRootViewController alloc] initWithNibName: nil bundle: nil];
// Instantiate the navigation bar
Self. navigationController = [[UINavigationController alloc] initWithRootViewController: self. zYRootViewController];
// Add a navigation bar to the current view
Self. window. rootViewController = self. navigationController;
// [Self. window addSubview: self. navigationController. view];
Return YES;
}
@ Synthesize navigationController;
@ Synthesize zYRootViewController;
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];
[Self. window makeKeyAndVisible];
Self. zYRootViewController = [[ZYRootViewController alloc] initWithNibName: nil bundle: nil];
// Instantiate the navigation bar
Self. navigationController = [[UINavigationController alloc] initWithRootViewController: self. zYRootViewController];
// Add a navigation bar to the current view
Self. window. rootViewController = self. navigationController;
// [Self. window addSubview: self. navigationController. view];
Return YES;
}
ZYRootViewController code
. M file:
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. title = @ "First"; // set the title
[Self defined mselector: @ selector (pushSecondController) withObject: nil afterDelay: 3.0f]; // call the pushSecondController method 3 seconds later
}
-(Void) pushSecondController {
ZYSecondViewController * zYSecondViewController = [[ZYSecondViewController alloc] initWithNibName: nil bundle: nil];
[Self. navigationController pushViewController: zYSecondViewController animated: YES]; // push zYSecondViewController View to the navigation bar Stack
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. title = @ "First"; // set the title
[Self defined mselector: @ selector (pushSecondController) withObject: nil afterDelay: 3.0f]; // call the pushSecondController method 3 seconds later
}
-(Void) pushSecondController {
ZYSecondViewController * zYSecondViewController = [[ZYSecondViewController alloc] initWithNibName: nil bundle: nil];
[Self. navigationController pushViewController: zYSecondViewController animated: YES]; // push zYSecondViewController View to the navigation bar Stack
} ZYSecondViewController code
. M file:
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. title = @ "Second ";
// Do any additional setup after loading the view.
[Self defined mselector: @ selector (goBack) withObject: nil afterDelay: 3.0f]; // call the goBack method 3 seconds later
}
-(Void) goBack {
[Self. navigationController popViewControllerAnimated: YES]; // bring your own view to the Navigation Pane stack.
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. title = @ "Second ";
// Do any additional setup after loading the view.
[Self defined mselector: @ selector (goBack) withObject: nil afterDelay: 3.0f]; // call the goBack method 3 seconds later
}
-(Void) goBack {
[Self. navigationController popViewControllerAnimated: YES]; // bring your own view to the Navigation Pane stack.
} Running result:
Initial Effect
Jump to the second view 3 seconds later:
This view will pop up three seconds later, showing the initial view: