你的位置:首页 > 软件开发 > 操作系统 > iOS Programming UINavigationController

iOS Programming UINavigationController

发布时间:2015-05-13 16:01:45
iOS Programming UINavigationController the Settings application has multiple related screens of information: a list of settings (like Soun ...

iOS Programming UINavigationController

iOS Programming UINavigationController

the Settings application has multiple related screens of information: a list of settings (like Sounds), a detailed page for each setting, and a selection page for each detail. This type of interface is called a drill-down interface.

setting application 有许多屏幕相关的信息:一系列的settings,每个setting的一个detail page和为每个detail page 的一个selection page。

这种类型的interface成为drill-down interface.

1 UINavigationController

When your application presents multiple screens of information, a UINavigationController maintains a stack of those screens.

当你的应用要展现多个屏幕信息的时候,UINavigationController保持了一个栈的这个样的屏幕。

Each screen is the view of a UIViewController,

每个屏幕就是一个UIViewController的view。

and the stack is an array of view controllers.

这个stack 就是一列view controller。

When a UIViewController is on top of the stack, its view is visible.

当一个UIViewController 在stack 的顶部,那么它的view 是可见的。

When you initialize an instance of UINavigationController, you give it one UIViewController. This UIViewController is the navigation controller's root view controller.

当你初始化UINavigationController,你就给它了一个UIViewController。这个UIViewController是navigationcontroller 的root view controller .

The root view controller is always on the bottom of the stack. More view controllers can be pushed on top of the UINavigationController's stack while the application is running.

The root view controller 总是在stack 的底部。当application运行的时候,更多地更控制被放在了UINavigationController's stack 的顶部。

When a UIViewController is pushed onto the stack, its view slides onto the screen from the right. When the stack is popped, the top view controller is removed from the stack and its view slides off to the right, exposing the view of next view controller on the stack.

当UIViewController被放进stack,它的view 将从屏幕的右边滑进来。当stack  is popped,the top view controller  将被移除,并且它的view 也将从右边滑出,展现出在stack 上的下一个view controller 

 

iOS Programming UINavigationController

 

The root view controller is the first object in the array. As more view controllers are pushed onto the stack, they are added to the end of this array.

The root view controller 是列的第一个对象。当更多地 view controllers被推送到stack 上,他们被添加到数组的最后。

Thus, the last view controller in the array is the top of the stack. UINavigationController's topViewController property keeps a pointer to the top of the stack.

因此数组的the last view controller  是stack 的顶部。UINavigationController's的topViewController property 有一个指针指向stack 的顶部。

 

UINavigationController is a subclass of UIViewController, so it has a view of its own. Its view always has two subviews: a UINavigationBar and the view of topViewController

UINavigationController是UIViewController的一个子类,因此它有一个他自己的view。它的view总是有两个subviews:一个是UINavigationBar,另一个是topViewController的view。

You can set a navigation controller as the rootViewController of the window to make its view a subview of the window.

你可以设置navigation controller为window的rootViewController,从而使它的view 成为window的subview。iOS Programming UINavigationController

iOS Programming UINavigationController

1.1

The only requirements for using a UINavigationController are that you give it a root view controller and add its view to the window.

你想用UINavigationController,你就把它给rootviewcontroller ,并把它的view 添加到window。

UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:itemViewController];

    self.window.rootViewController=navigationController;

2 An Additional UIViewController

Create a new Objective-C class (File → New → File...). Name this class BNRDetailViewController and choose UIViewController as the superclass. Check the With XIB for user interface box

iOS Programming UINavigationController

iOS Programming UINavigationController

iOS Programming UINavigationController

3 Navigating with UINavigationController

          3.1 Pushing view controllers

The viewControllers array of a navigation controller is dynamic – you start with a root view controller and push view controllers depending on user input.

The viewControllers array of a navigation controller是动态的 -你开始一个root view controller 并且根据你的输入,推送view Controller 。

Therefore, some object other than the navigation controller needs to create the instance of BNRDetailViewController and be responsible for adding it to the stack.

因此,除了navigation controller 之外还有其他的对象需要创建BNRDetailViewController的实例,并负责把它添加到stack 中。

 

This object must meet two requirements: it needs to know when to push a BNRDetailViewController onto the stack, and it needs a pointer to the navigation controller to send the navigation controller messages, namely, pushViewController:animated:.

这个对象需要满足两个要求(1) 它需要知道什么时候把BNRDetailViewController放入到stack 上。(2) 它需要指针指向 navigation controller 发送navigation controller信息,名字为pushViewController:animated。 

 

BNRItemsViewController fills both requirements. First, it knows when a row is tapped in a table view because, as the table view's delegate, it receives the message tableView:didSelectRowAtIndexPath: when this event occurs.

BNRItemsViewController满足以上两条(1) 它知道什么时候在table view中的一行被选中,作为table view的委托,当这个时间发生时,它接受tableView:didSelectRowAtIndexPath的消息。

Second, any view controller in a navigation controller's stack can get a pointer to that navigation controller by sending itself the message navigationController.As the root view controller, BNRItemsViewController is always in the navigation controller's stack and thus can always access it.

(2)任何 在navigation controller's stack中view controller 都有一个指向navigation controller的指针,通过向自己发送navigationController消息。

 

Therefore, BNRItemsViewController will be responsible for creating the instance of BNRDetailViewController and adding it to the stack.

#import "BNRDetailViewController.h"

- (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]

}

Since the UINavigationController's stack is an array, it will take ownership of any view controller added to it.

因为UINavigationController's stack 是一个数组,它可以拥有任意类型的view Controller。

Thus, the BNRDetailViewController is owned only by the UINavigationController after tableView:didSelectRowAtIndexPath: finishes. When the stack is popped, the BNRDetailViewController is destroyed. The next time a row is tapped, a new instance of BNRDetailViewController is created.

因此BNRDetailViewController仅仅当tableView:didSelectRowAtIndexPath完成后才被UINavigationController拥有。当这个stack 被poped ,BNRDetailViewController将被销毁。

          3.2 Passing data between view controllers

To fill these fields, you need a way to pass the selected BNRItem from the BNRItemsViewController to the BNRDetailViewController.

为了填充空白,你需要用一种方式把选中的从BNRItemsViewController 得到的BNRItem 传递给BNRDetailViewController。

To pull this off, you will give BNRDetailViewController a property to hold a BNRItem.

为了胜利完成这个,你需要给BNRDetailViewController一个属性来拥有BNRItem。

When a row is tapped, BNRItemsViewController will give the corresponding BNRItem to the instance of BNRDetailViewController that is being pushed onto the stack.

当一行被选中后,BNRItemsViewController将给对应的BNRItem传递给将要进入stack的BNRDetailViewController。

The BNRDetailViewController will populate its text fields with the properties of that BNRItem.

BNRDetailViewController将会显示拥有那个BNRItem属性的text field .

Editing the text in the text fields on BNRDetailViewController's view will change the properties of that BNRItem.

编辑在BNRDetailViewController's view中 text field 中得text将会改变那个BNRItem的属性。

#import <UIKit/UIKit.h>很困惑如何保存到[BNRItemStore sharedStore]的。 By default, a UINavigationItem is empty.In BNRItemsViewController.m, modify init to set the navigationItem's title to read Homepwner.It would be nice to have the BNRDetailViewController's navigation item title be the name of the BNRItem it is displaying. Obviously, you cannot do this in init because you do not yet know what its item will be.4.1 There are three customizable areas for each UINavigationItem: a leftBarButtonItem, a rightBarButtonItem, and a titleView.Like UINavigationItem, UIBarButtonItem is not a subclass of UIView.A bar button item has a target-action pair that works like UIControl's target-action mechanism: when tapped, it sends the action message to the target.let's add another UIBarButtonItem to replace the Edit button in the table view header. In BNRItemsViewController.m, edit the init method.5 bug

bug:Cannot find executable for CFBundle 0x8f92720 </Applications/xcode5/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)

 

解决办法:Temporary workaround: click iOS Simulator > Reset Content and Settings... and run again.

原标题:iOS Programming UINavigationController

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。