您的位置:首页 > 产品设计 > UI/UE

第十章 UINavigationController 翻译

2016-05-09 16:33 603 查看
一,可以用于创建有关联关系的页面

二,UINavigationController

        1,UINavigationController维护一个多屏幕的堆栈,每个screen都是一个viewController

        2,UINavigationController继承于viewController,它的view属性包含一个UINavigationBar和topViewController的view;

        3,可以直接将UINavigationController添加到window中,这样也可以做到UINavigationController的切换

        

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]];
// Override point for customization after application launch
BNRItemsViewController *itemsViewController
= [[BNRItemsViewController alloc] init];
// Create an instance of a UINavigationController
// its stack contains only itemsViewController
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:itemsViewController];
// Place navigation controller's view in the window hierarchy
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}


四,使用UINavigationController进行页面导航

       1,初始化UINavigationController必须有一个controller

       2,可以使用controller的navigationController访问UINavigationController

       3,可以使用navigationController的pushViewController:animated:添加一个controller

@implementation BNRItemsViewController
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController =
[[BNRDetailViewController alloc] init];
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}


       4,所有加入到navigationController的controller构成一个堆栈关系

五,在controller之间传递数据

        1,可以直接在controller之间传递数据

        2,一般可以将数据都放在root controller中,并传递到下一级的controller

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController =
[[BNRDetailViewController alloc] init];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.item = selectedItem;
[self.navigationController pushViewController:detailViewController
animated:YES];
}


       

六,隐藏或显示views

        1,使用[self.view endEditing:YES];来释放focus,并隐藏键盘

        2,使用self.tableView reloadData来刷新tableView的显示

七,UINavigationBar

        1,UIViewController有一个UINavigationItem的属性,navigationController使用它来初始化导航条

        2,手动创建action连接

               

- (instancetype)init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = @"Homepwner";
// Create a new bar button item that will send
// addNewItem: to BNRItemsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
}
return self;
}


       3,连接编辑模式

             navItem.leftBarButtonItem = self.editButtonItem;

             viewController访问editButtonItem时候,会创建并返回UIBarButtonItem

       4,UINavigationItem不是一个view

一,可以用于创建有关联关系的页面

二,UINavigationController

        1,UINavigationController维护一个多屏幕的堆栈,每个screen都是一个viewController

        2,UINavigationController继承于viewController,它的view属性包含一个UINavigationBar和topViewController的view;

        3,可以直接将UINavigationController添加到window中,这样也可以做到UINavigationController的切换

        

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]];
// Override point for customization after application launch
BNRItemsViewController *itemsViewController
= [[BNRItemsViewController alloc] init];
// Create an instance of a UINavigationController
// its stack contains only itemsViewController
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:itemsViewController];
// Place navigation controller's view in the window hierarchy
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}


四,使用UINavigationController进行页面导航

       1,初始化UINavigationController必须有一个controller

       2,可以使用controller的navigationController访问UINavigationController

       3,可以使用navigationController的pushViewController:animated:添加一个controller

@implementation BNRItemsViewController
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController =
[[BNRDetailViewController alloc] init];
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}


       4,所有加入到navigationController的controller构成一个堆栈关系

五,在controller之间传递数据

        1,可以直接在controller之间传递数据

        2,一般可以将数据都放在root controller中,并传递到下一级的controller

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController =
[[BNRDetailViewController alloc] init];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.item = selectedItem;
[self.navigationController pushViewController:detailViewController
animated:YES];
}


       

六,隐藏或显示views

        1,使用[self.view endEditing:YES];来释放focus,并隐藏键盘

        2,使用self.tableView reloadData来刷新tableView的显示

七,UINavigationBar

        1,UIViewController有一个UINavigationItem的属性,navigationController使用它来初始化导航条

        2,手动创建action连接

               

- (instancetype)init
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = @"Homepwner";
// Create a new bar button item that will send
// addNewItem: to BNRItemsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
}
return self;
}


       3,连接编辑模式

             navItem.leftBarButtonItem = self.editButtonItem;

             viewController访问editButtonItem时候,会创建并返回UIBarButtonItem

       4,UINavigationItem不是一个view
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: