分享

UINavigationController- 知识总结

 最初九月雪 2014-12-18

很多时候,喜欢用到一个知识,就要将它的原理搞清楚。
昨天,搞了很久才搞定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 *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

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


    // 广场

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

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

    // 设置广场-TabBar

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

    squareViewController.tabBarItem=itemSquare;

    [itemSquare release];

    [squareViewController release];

    

    // 视频

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

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

    

    // 设置视频-TabBar

    UITabBarItem *itemVideo= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

    videoViewController.tabBarItem=itemVideo;

    [itemVideo release];

    

    [videoViewController release];

    // 播放器

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

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

    

    // 设置播放器-TabBar

    UITabBarItem *itemShoot= [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:0];

    shootViewController.tabBarItem=itemShoot;

    [itemShoot release];

    

    [shootViewController release];

    

    // 会员信息

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

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

    

    // 设置会员-TabBar

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

    selfInfoViewController.tabBarItem=itemInfo;

    [itemInfo release];

    

    [selfInfoViewController release];

    

    // 更多

    MoreViewController *moreViewController=[[MoreViewController alloc] init];

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

    

    // 设置更多-TabBar

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

    moreViewController.tabBarItem=itemMore;

    [itemMore release];

    

    [moreViewController release];

    

    // 加入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.window makeKeyAndVisible];


    

    return YES;

}


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

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

运行如下:


UINavigationController-知识总结

于是,要问:为什么第一个页面要显示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-知识总结


关于UINavigationController一目了然!

希望对你有所帮助!




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多