iOS Programming UINavigationController,ioscontroller跳轉

來源:互聯網
上載者:User

iOS Programming UINavigationController,ioscontroller跳轉

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 

 

 

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。

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

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>
@class BNRItem;

@interface BNRDetailViewController : UIViewController

@property (nonatomic, strong) BNRItem *item;

@end

 

When the BNRDetailViewController's view appears on the screen, it needs to set up its subviews to show the properties of the item.

當BNRDetailViewController's的view 顯示在螢幕上時,它需要設定它的subview 來顯示item的屬性

- (void)viewWillAppear:(BOOL)animated

{
[super viewWillAppear:animated];

BNRItem *item = self.item;

self.nameField.text = item.itemName; self.serialNumberField.text = item.serialNumber; self.valueField.text = [NSString stringWithFormat:@"%d",

item.valueInDollars];

// You need an NSDateFormatter that will turn a date into a simple date string

static NSDateFormatter *dateFormatter = nil; if (!dateFormatter) {

dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateStyle = NSDateFormatterMediumStyle; dateFormatter.timeStyle = NSDateFormatterNoStyle;

}

// Use filtered NSDate object to set dateLabel contents

self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated]; }

 

In BNRItemsViewController.m, add the following code to tableView:didSelectRowAtIndexPath: so that BNRDetailViewController has its item before viewWillAppear: gets called.

 

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;

 

          3.3how data is passed between view controllers?

Having all of the data in the root view controller and passing subsets of that data to the next UIViewController (like you just did) is a clean and efficient way of performing this task.

讓root view controller 擁有所有資料,然後傳遞給下一個UIViewController是一個有效和乾淨的方式傳遞這些資料。

 

          3.4 Appearing and disappearing views

Whenever a UINavigationController is about to swap views, it sends out two messages: viewWillDisappear: and viewWillAppear:.

當UINavigationController將要切換view時,它會發送兩個訊息viewWillDisappear和viewWillAppear。

The UIViewController that is about to be popped off the stack is sent the message viewWillDisappear:. The UIViewController that will then be on top of the stack is sent viewWillAppear:.

那個將要從stack 中popped 的UIViewController被發送為viewWillDisappear。那個將要添加到stack 頂部的UIViewController將會被發送viewWillAppear。

When a BNRDetailViewController is popped off the stack, you will set the properties of its item to the contents of the text fields.

當一個BNRDetailViewController從stack刪除的時候,它就會設定它的item屬性為text field 的內容。

When implementing these methods for views appearing and disappearing, it is important to call the superclass's implementation – it might have some work to do and needs to be given the chance to do it.

當實現這些方法的時候,調用superclass 的實現很重要。它可能有一些工作要做,你應該給它這個機會。

- (void)viewWillDisappear:(BOOL)animated

{
[super viewWillDisappear:animated];

// Clear first responder [self.view endEditing:YES];

// "Save" changes to item

BNRItem *item = self.item;

item.itemName = self.nameField.text; item.serialNumber = self.serialNumberField.text; item.valueInDollars = [self.valueField.text intValue];

     3.4.1

Notice the use of endEditing:. When the message endEditing: is sent to a view, if it or any of its subviews is currently the first responder, it will resign its first responder status, and the keyboard will be dismissed. (The argument passed determines whether the first responder should be forced into retirement. Some first responders might refuse to resign, and passing YES ignores that refusal.)

主要到endEditing得使用。

當endEditing 被發給一個view ,那麼這個view 以及它的任意subviews 如果是 currently first  responder,它將resign  first responder 的狀態,並且keyboard將會消失。

 

Now the values of the BNRItem will be updated when the user taps the Back button on the UINavigationBar.
當使用者tap  back button ,那麼BNRItem將會更新。
When BNRItemsViewController appears back on the screen, it is sent the message viewWillAppear:. Take this opportunity to reload the UITableView so the user can immediately see the changes.
當BNRItemsViewController顯示到螢幕上時,它會被發送一個viewWillAppear的訊息。這是一個重新載入UITableView資料的機會。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData]; }

很困惑如何儲存到[BNRItemStore sharedStore]的。 
4 . UINavigationBar
Every UIViewController has a navigationItem property of type UINavigationItem. However, unlike UINavigationBar, UINavigationItem is not a subclass of UIView, so it cannot appear on the screen.
每一個UIViewController都有一個UINavigationItem類型的navigationItem 屬性。與UINavigationBar不同的是UINavigationItem並不是UIView 的子類,所以它不能顯示在螢幕上。
Instead, the navigation item supplies the navigation bar with the content it needs to draw.
取而代之的是navigation item提供了navigation bar 需要繪製 的內容。
When a UIViewController comes to the top of a UINavigationController's stack, the UINavigationBar uses the UIViewController's navigationItem to configure itself
當一個UIViewController在UINavigationController'的頂部時,這個UINavigationBar用UIViewController的navigationItem來配置自己。

By default, a UINavigationItem is empty.
預設情況下,一個UINavigationItem是空的。
At the most basic level, a UINavigationItem has a simple title string.
UINavigationItem有一個簡單地標題字串。
When a UIViewController is moved to the top of the navigation stack and its navigationItem has a valid string for its title property, the navigation bar will display that string
當UIViewController被移動到navigation stack 頂部時,它的navigationItem有一個有效string,那麼navigation bar將會顯示這個 string。
In BNRItemsViewController.m, modify init to set the navigationItem's title to read Homepwner.
- (instancetype)init
{
self = [super initWithStyle:UITableViewStylePlain]; if (self) {
UINavigationItem *navItem = self.navigationItem; navItem.title = @"Homepwner";
}
return self; }

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.
明顯的,你不能在init 方法中賦值,因為這時,你還不知道它的item是什麼。
, the BNRDetailViewController will set its title when it sets its item property. In BNRDetailViewController.m, implement setItem:, replacing the synthesized setter method for item.
你可以重寫setItem,在setItem中重寫該方法。
- (void)setItem:(BNRItem *)item
{
_item = item;
self.navigationItem.title = _item.itemName; }

4.1 There are three customizable areas for each UINavigationItem: a leftBarButtonItem, a rightBarButtonItem, and a titleView.
每個UINavigationItem有三個通用的地區:leftBarButtonItem,rightBarButtonItem,titleView。
The left and right bar button items are pointers to instances of UIBarButtonItem, which contains the information for a button that can only be displayed on a UINavigationBar or a UIToolbar.
左邊和右邊的bar button items 有指標指向UIBarButtonItem的執行個體。該執行個體包含了只能在UINavigationBar或者是UIToolbar上顯示的button 資訊。

Like UINavigationItem, UIBarButtonItem is not a subclass of UIView.
UIBarButtonItem像UINavigationItem也不是UIView 的子類。
Instead, UINavigationItem encapsulates information that UINavigationBar uses to configure itself.
UINavigationItem概況了UINavigationBar配置自己需要的資訊。
Similarly, UIBarButtonItem is not a view, but holds the information about how a single button on the UINavigationBar should be displayed.
相似的,UIBarButtonItem也不是個view,但是擁有怎樣在UINavigationBar顯示的一個button的資訊。
(A UIToolbar also uses instances of UIBarButtonItem to configure itself.)
一個UIToolBar 也是用UIBarButtonItem的執行個體來配置自己。
The third customizable area of a UINavigationItem is its titleView.
第三個適應地區是它的titleView。 
You can either use a basic string as the title or have a subclass of UIView sit in the center of the navigation item. You cannot have both.
你可以用一個基本的string作為titile或者有一個UIView 的子類在navigation item 的中心。你不能同時有這兩個。

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.
一個bar button item  有一個target-action pair就像UIcontrol 的target-action 機制:當點擊時,它發送一個action 訊息給target.

let's add another UIBarButtonItem to replace the Edit button in the table view header. In BNRItemsViewController.m, edit the init method.
navItem.leftBarButtonItem = self.editButtonItem;
Where does editButtonItem come from?
這個editButtonItem從哪來的?
UIViewController has an editButtonItem property, and when sent editButtonItem, the view controller creates a UIBarButtonItem with the title Edit.
Even better, this button comes with a target- action pair: it sends the message setEditing:animated: to its UIViewController when tapped.
這個button有一個target-action pair:當點擊的時候它發送setEditing:animated給UIViewController。 

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.
這個錯誤會隨機出現。這應該是Xcode 的一個錯誤吧。

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.