IOS 导航控制器基本使用(UINavigationController代码创建方式)

● UINavigationController的使用步骤

➢ 初始化UINavigationController

➢ 设置UIWindow的rootViewController为UINavigationController

➢ 根据具体情况,通过push方法添加对应个数的子控制器

UINavigationController的子控制器

● UINavigationController以栈的形式保存子控制器

@property(nonatomic,copy) NSArray *viewControllers;

@property(nonatomic,readonly) NSArray *childViewControllers;

● 使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

● 使用pop方法可以移除控制器

➢ 将栈顶的控制器移除

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

➢ 回到指定的子控制器

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

➢ 回到根控制器(栈底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

修改导航栏的内容

● 导航栏的内容由栈顶控制器的navigationItem属性决定

● UINavigationItem有以下属性影响着导航栏的内容
➢ 左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;

➢ 中间的标题视图

@property(nonatomic,retain) UIView *titleView;

➢ 中间的标题文字

@property(nonatomic,copy) NSString *title;

➢ 左上角的视图

@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

➢ UIBarButtonItem *rightBarButtonItem 右上角的视图

@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

初级方式 1

@implementation NJAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; // 1.创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] init];
// 2.设置导航控制器为window的根控制器
self.window.rootViewController = nav; // 创建第一个控制器并添加到导航控制器中
NJOneViewController *one = [[NJOneViewController alloc] init];
[nav pushViewController:one animated:YES]; /*
// 3.添加子控制器到导航控制器中
// 创建一些子控制器
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor]; UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor greenColor]; UIViewController *vc3 = [[UIViewController alloc] init];
vc3.view.backgroundColor = [UIColor purpleColor]; // 添加子控制器到导航控制器中
[nav pushViewController:vc1 animated:YES];
[nav pushViewController:vc2 animated:YES];
[nav pushViewController:vc3 animated:YES];
*/ [self.window makeKeyAndVisible];
return YES;
}
@interface NJOneViewController ()
/**
* 跳转到第二个控制器
*/
- (IBAction)jump2Two:(id)sender; @end @implementation NJOneViewController - (IBAction)jump2Two:(id)sender { // 1.创建第二个控制器
NJTwoViewController *two = [[NJTwoViewController alloc] init];
// 2.将第二个控制器添加到导航控制器中
// 只要当前控制器是导航控制器的子控制器,就快要通过该属性直接获取到当前控制器所在的导航控制器
[self.navigationController pushViewController:two animated:YES];
}

中级方式

@implementation NJAppDelegate

// 应用程序启动完毕即会调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; // 1.创建导航控制器
NJOneViewController *one = [[NJOneViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:one]; // 2.设置导航控制器为window的根控制器
self.window.rootViewController = nav; // 创建第一个控制器并添加到导航控制器中
// NJOneViewController *one = [[NJOneViewController alloc] init];
// 第一种
// [nav pushViewController:one animated:YES]; // 导航控制器的栈
// nav.viewControllers; == nav.childViewControllers;
// 第二种
// [nav addChildViewController:one]; // 第三种
// nav.viewControllers = @[one]; // 注意该属性是只读的
// nav.childViewControllers = @[one]; // NSLog(@"%d - %d", nav.viewControllers.count, nav.childViewControllers.count); // 获取栈顶的控制器
// NSLog(@"%@", nav.topViewController); // 1.取出导航控制器的导航条 [self.window makeKeyAndVisible]; NSLog(@"didFinishLaunchingWithOptions");
return YES;
} // 应用程序获取焦点(代表着可以和用户交互)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"applicationDidBecomeActive"); UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
UINavigationBar *bar = nav.navigationBar;
// NSLog(@"%@", NSStringFromCGRect(bar.frame)); NSString *str = [self digView:self.window];
[str writeToFile:@"/Users/apple/Desktop/ios6.xml" atomically:YES]; } /**
* 返回传入veiw的所有层级结构
*
* @param view 需要获取层级结构的view
*
* @return 字符串
*/
- (NSString *)digView:(UIView *)view
{
if ([view isKindOfClass:[UITableViewCell class]]) return @"";
// 1.初始化
NSMutableString *xml = [NSMutableString string]; // 2.标签开头
[xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)];
if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
[xml appendFormat:@" bounds=\"%@\"", NSStringFromCGRect(view.bounds)];
} if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView *)view;
if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
[xml appendFormat:@" contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)];
}
} // 3.判断是否要结束
if (view.subviews.count == ) {
[xml appendString:@" />"];
return xml;
} else {
[xml appendString:@">"];
} // 4.遍历所有的子控件
for (UIView *child in view.subviews) {
NSString *childXml = [self digView:child];
[xml appendString:childXml];
} // 5.标签结尾
[xml appendFormat:@"</%@>", view.class]; return xml;
}
/**
* 跳转到第二个控制器
*/
- (IBAction)jump2Two:(id)sender; @end @implementation NJOneViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 控制当前控制器对应的导航条显示的内容
self.navigationItem.title = @"第一个控制器7788"; // 给导航条添加一个按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil]; // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil]; UIBarButtonItem *item0 =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil]; UIBarButtonItem *item1 =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; self.navigationItem.rightBarButtonItems = @[item0, item1]; // 修改返回按钮显示的内容
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]; } - (IBAction)jump2Two:(id)sender { // 1.创建第二个控制器
NJTwoViewController *two = [[NJTwoViewController alloc] init];
// 2.将第二个控制器添加到导航控制器中
// 只要当前控制器是导航控制器的子控制器,就快要通过该属性直接获取到当前控制器所在的导航控制器
[self.navigationController pushViewController:two animated:YES];
}
上一篇:IOS开发UI篇—导航控制器属性和基本使用


下一篇:ActionLink()与jquery更好地结合建造MVC网页: