本文主要是介绍IOS开发(7)之UINavigationController导航,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 前言
UINavigationController会让App从一个试图控制器切换到另一个,在开发中十分常见,今天我们来学习一下这个控件。
2 UINavigation简介
delegate代码
.h文件:
#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文件:
@synthesize navigationController;
@synthesize zYRootViewController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(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];//实例化导航栏self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.zYRootViewController];//向当前视图添加导航栏self.window.rootViewController = self.navigationController;
// [self.window addSubview:self.navigationController.view];return YES;
}
ZYRootViewController代码
.m文件:
- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view.self.title = @"First";//设置标题[self performSelector:@selector(pushSecondController) withObject:nil afterDelay:3.0f];//3秒后调用自身pushSecondController方法
}-(void)pushSecondController{ZYSecondViewController *zYSecondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil];[self.navigationController pushViewController:zYSecondViewController animated:YES];//向导航栏堆栈推入zYSecondViewController视图
}
ZYSecondViewController代码
.m文件:
- (void)viewDidLoad
{[super viewDidLoad];self.title = @"Second";// Do any additional setup after loading the view.[self performSelector:@selector(goBack) withObject:nil afterDelay:3.0f];//3秒后调用自身goBack方法
}-(void)goBack{[self.navigationController popViewControllerAnimated:YES];//将自身视图弹出导航栏堆栈
}
运行结果:
最初的效果
3秒以后跳到第二个视图:
又3秒以后弹出本视图,显示初始视图:
3 结语
以上就是UINavigation的简单介绍,希望对大家有所帮助。‘
Demo实例下载地址:http://download.csdn.net/detail/u010013695/5291697
这篇关于IOS开发(7)之UINavigationController导航的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!