IOS Programming UINavigationController, ioscontroller jump

Source: Internet
Author: User
Tags uicontrol

IOS Programming UINavigationController, ioscontroller jump

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.

The setting application has a lot of screen-related information: A series of settings, one detail page for each setting and one selection page for each detail page.

This type of interface becomes the drill-down interface.

1 UINavigationController

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

When your application needs to display multiple screen information, UINavigationController keeps the screen of a stack.

Each screen is the view of a UIViewController,

Each screen is a view of UIViewController.

And the stack is an array of view controllers.

This stack is a list of view controllers.

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

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

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

When you initialize UINavigationController, you give it a UIViewController. This UIViewController is the root view controller of navigationcontroller.

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 is always at The bottom of The stack. When the application is running, more control is placed on the top of the 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.

When UIViewController is put into the stack, its view will slide in from the right of the screen. When the stack is popped, the top view controller will be removed, and its view will also slide out from the right side, showing the next view controller on the stack.

 

 

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 is The first object in The column. When more view controllers are pushed to the stack, they are added to the end of the array.

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.

Therefore, the last view controller of the array is the top of the stack. The topViewController property OF UINavigationController's has a pointer pointing to the top of the 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 is a subclass of UIViewController, so it has its own view. Its view always has two subviews: one is UINavigationBar, and the other is the view of topViewController.

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

You can set the navigation controller as the rootViewController of the window so that its view becomes the subview of the window.

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.

If you want to use UINavigationController, you can give it to rootviewcontroller and add its view to 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 is dynamic-you start a root view controller and push The view Controller Based on your input.

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

Therefore, in addition to navigation controller, other objects need to create an instance of BNRDetailViewController and add it to the 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 :.

This object must meet two requirements: (1) it needs to know when to put BNRDetailViewController into the stack. (2) It needs a pointer to the navigation controller to send the navigation controller information, whose name is 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 reads es the message tableView: didSelectRowAtIndexPath: when this event occurs.

BNRItemsViewController meets the preceding two requirements: (1) it knows when a row in table view is selected as the delegate of table view. When this happens, it accepts the message 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) Any view controller in the navigation controller's stack has a pointer to the navigation controller by sending a navigationController message to itself.

 

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.

Because UINavigationController's stack is an array, it can have any type of 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.

Therefore, BNRDetailViewController is owned by UINavigationController only when tableView: didSelectRowAtIndexPath is complete. When this stack is poped, BNRDetailViewController will be destroyed.

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.

To fill in the blank space, you need to pass the BNRItem obtained from BNRItemsViewController to BNRDetailViewController in one way.

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

To achieve this, you need to give BNRDetailViewController an attribute to own BNRItem.

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

After a row is selected, BNRItemsViewController will pass the corresponding BNRItem to the BNRDetailViewController that will enter the stack.

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

BNRDetailViewController will display the text field with the BNRItem attribute.

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

Editing text in the text field in the BNRDetailViewController's view changes the attributes of the 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.

When BNRDetailViewController's view is displayed on the screen, it needs to set its subview to display item attributes.

-(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 making this task.

Letting the root view controller have all the data, and then passing it to the next UIViewController is an effective and clean way to pass the data.

 

3.4 Appearing and disappearing views

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

When UINavigationController is about to switch the view, it will send two messages: viewWillDisappear and 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 :.

The UIViewController that will be popped from the stack is sent as viewWillDisappear. The UIViewController to be added to the top of the stack will be sent to viewWillAppear.

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

When a BNRDetailViewController is deleted from the stack, it sets its item attribute to 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.

When implementing these methods, it is important to call the superclass implementation. It may have some work to do. You should give it the opportunity.

-(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 shocould be forced into retirement. some first responders might refuse to resign, and passing YES ignores that refusal .)

Mainly used in endEditing.

When endEditing is sent to a view, if this view and any of its subviews are currently first responder, it will resign the first responder status and the keyboard will disappear.

 

Now the values of the BNRItem will be updated when the user taps the Back button on the UINavigationBar.
When you tap back button, BNRItem is updated.
When BNRItemsViewController appears back on the screen, it is sent the message viewWillAppear:. Take this opportunity to reload the UITableView so the user can be immediately see the changes.
When BNRItemsViewController is displayed on the screen, it sends a viewWillAppear message. This is an opportunity to reload UITableView data.
-(Void) viewWillAppear :( BOOL) animated
{
[Super viewWillAppear: animated];
[Self. tableView reloadData];}

I am confused about how to save it to [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.
Each UIViewController has a navigationItem attribute of the UINavigationItem type. Unlike UINavigationBar, UINavigationItem is not a subclass of UIView, so it cannot be displayed on the screen.
Instead, the navigation item supplies the navigation bar with the content it needs to draw.
Instead, the navigation item provides the content to be drawn in the navigation bar.
When a UIViewController comes to the top of a UINavigationController's stack, the UINavigationBar uses the UIViewController's navigationItem to configure itself
When a UIViewController is at the top of UINavigationController, The UINavigationBar uses the navigationItem of UIViewController to configure itself.

By default, a UINavigationItem is empty.
By default, a UINavigationItem is empty.
At the most basic level, a UINavigationItem has a simple title string.
UINavigationItem has a simple title string.
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
When UIViewController is moved to the top of the navigation stack, its navigationItem has a valid string, and the navigation bar displays this 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 wocould be nice to have the BNRDetailViewController's navigation item title be the name of the BNRItem it is displaying. obviusly, you cannot do this in init because you do not yet know what its item will be.
Obviously, you cannot assign values in the init method, because at this time, you do not know what its item is.
, The BNRDetailViewController will set its title when it sets its item property. In BNRDetailViewController. m, implement setItem:, replacing the synthesized setter method for item.
You can override setItem and rewrite this method in 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.
Each UINavigationItem has three common regions: leftBarButtonItem, rightBarButtonItem, and 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 buttons on the left and right have pointers to UIBarButtonItem instances. This instance contains the button information that can only be displayed on UINavigationBar or UIToolbar.

Like UINavigationItem, UIBarButtonItem is not a subclass of UIView.
UIBarButtonItem, like UINavigationItem, is not a subclass of UIView.
Instead, UINavigationItem encapsulates information that UINavigationBar uses to configure itself.
UINavigationItem provides an overview of what UINavigationBar needs to configure.
Similarly, UIBarButtonItem is not a view, but holds the information about how a single button on the UINavigationBar shocould be displayed.
Similarly, UIBarButtonItem is not a view, but has the information about how to display a button in UINavigationBar.
(A UIToolbar also uses instances of UIBarButtonItem to configure itself .)
A UIToolBar also uses the UIBarButtonItem instance to configure itself.
The third customizable area of a UINavigationItem is its titleView.
The third adaptive area is its 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.
You can use a basic string as the titile or a subclass of UIView in the center of the navigation item. You cannot have both.

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.
A bar button item has a target-action pair, which is like the target-action Mechanism of UIcontrol. When you click it, it sends an action message to 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?
Where does this editButtonItem come from?
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.
This button has a target-action pair: when clicked, it sends setEditing: animated to UIViewController.

5 bugs

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)

 

Solution: Temporary workaround: click iOS Simulator> Reset Content and Settings... and run again.
This error will occur randomly. This should be an error of Xcode.

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.