打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
UINavigationController- 知识总结

很多时候,喜欢用到一个知识,就要将它的原理搞清楚。
昨天,搞了很久才搞定UINavigationController相关的一个功能。
发现网上对UINavigationController讲解的很少,虽然,苹果官方有说明,但是都是英文的。而且长篇大论的,总希望能够“刨根问底”!
于是,写下来mark一下!

在开始之前,首先做一个程序的框架:


1.用UITabBarController+UINavigationController制作一个原生态的程序框架


@interface AppDelegate : UIResponder <</span>UIApplicationDelegate>{

    UITabBarController *tabBarController;

    

    UINavigationController*navSquareController;

    UINavigationController*navMoreController;

    UINavigationController*navSelfInfoController;

    UINavigationController*navShootController;

    UINavigationController*navVideoController;

}


@property (nonatomic,retain) UIWindow *window;

@property (strong, retain) UITabBarController *tabBarController;


@property (strong, retain) UINavigationController*navSquareController;

@property (strong, retain) UINavigationController*navMoreController;

@property (strong, retain) UINavigationController*navSelfInfoController;

@property (strong, retain) UINavigationController*navShootController;

@property (strong, retain) UINavigationController*navVideoController;


@end


-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window= [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


   // 广场

   SquareViewController *squareViewController=[[SquareViewControlleralloc] initWithNibName:@"SquareViewController" bundle:nil];

    navSquareController=[[UINavigationController alloc] initWithRootViewController:squareViewController];

   // 设置广场-TabBar

   UITabBarItem *itemSquare= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];

   squareViewController.tabBarItem=itemSquare;

    [itemSquare release];

    [squareViewControllerrelease];

    

   // 视频

   VideoViewController *videoViewController=[[VideoViewControlleralloc] initWithNibName:@"VideoViewController" bundle:nil];

    navVideoController=[[UINavigationController alloc] initWithRootViewController:videoViewController];

    

   // 设置视频-TabBar

   UITabBarItem *itemVideo= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:0];

   videoViewController.tabBarItem=itemVideo;

    [itemVideo release];

    

    [videoViewControllerrelease];

   // 播放器

   ShootViewController *shootViewController=[[ShootViewControlleralloc] initWithNibName:@"ShootViewController" bundle:nil];

    navShootController=[[UINavigationController alloc] initWithRootViewController:shootViewController];

    

   // 设置播放器-TabBar

   UITabBarItem *itemShoot= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostRecenttag:0];

   shootViewController.tabBarItem=itemShoot;

    [itemShoot release];

    

    [shootViewControllerrelease];

    

   // 会员信息

   SelfInfoViewController *selfInfoViewController=[[SelfInfoViewControlleralloc] initWithNibName:@"SelfInfoViewController" bundle:nil];

    navSelfInfoController=[[UINavigationController alloc] initWithRootViewController:selfInfoViewController];

    

   // 设置会员-TabBar

   UITabBarItem *itemInfo= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];

   selfInfoViewController.tabBarItem=itemInfo;

    [itemInfo release];

    

    [selfInfoViewControllerrelease];

    

   // 更多

   MoreViewController *moreViewController=[[MoreViewControlleralloc] init];

    navMoreController=[[UINavigationController alloc] initWithRootViewController:moreViewController];

    

   // 设置更多-TabBar

   UITabBarItem *itemMore= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];

   moreViewController.tabBarItem=itemMore;

    [itemMore release];

    

    [moreViewControllerrelease];

    

   // 加入tabBarController

   self.tabBarController = [[[UITabBarController alloc] init]autorelease];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:navSquareController,navVideoController,navShootController,navSelfInfoController,navMoreController, nil];

   self.window.rootViewController = self.tabBarController;

    

   [self.window addSubview:tabBarController.view];

   [self.windowmakeKeyAndVisible];


    

   return YES;

}


这个是原生态的一个框架。可以来做应用程序的公用框架!

我们的程序一般都是这样!经典啊!

运行如下:




于是,要问:为什么第一个页面要显示navSquareController


self.tabBarController.viewControllers = [NSArray arrayWithObjects:navSquareController,navVideoController,navShootController,navSelfInfoController,navMoreController, nil];

我们将navSquareController排在数组第一个元素,所以,默认显示navSquareController


为什么navSquareController又显示的是squareViewController


navSquareController=[[UINavigationController alloc] initWithRootViewController:squareViewController];

squareViewController作为navSquareController的根视图控制器!

因为我们还没有对navSquareController进行任何pushViewController操作,所以,默认显示的是squareViewController


做了这么多准备工作,现在可以切入正题!


1.什么是UINavigationController


UINavigationController继承自UIViewController

UINavigationController管理栈中的视图控制器UIViewController(多个),和一个UINavigationBar和一个可选的UIToolbar

负责管理多个UIViewController之间的水平方向的push(压入)和pop(弹出),并且同步修改其UINavigationBar



一句话:UINavigationController是用来管理多个UIViewController之间的导航控制和显示UINavigationBar *_navigationBarUIToolbar*_toolbar


最后,附上苹果帮助文档中的,关于UINavigationController的结构图:





关于UINavigationController一目了然!

希望对你有所帮助!




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
UITabBarController
UITabBarController的使用总结
UINavigationController详解
iphone开发导航控制器的使用
iOS 各种传值方式
UINavigationController学习
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服