IOS5 storyboard (3)

Source: Internet
Author: User
It's time to add more viewcontrollers to our storyboards. We will create a scenario so that users can add new players to the program. Segues introduction

It's time to add more viewcontrollers to our storyboards. We will create a scenario so that users can add new players to the program.

Drag a BarButtonItem to the right of the Players scene navigation bar. Use the property panel to change its Identifier to Add, so that it becomes a standard + number button. When you click this button, a new mode window is displayed, where you can enter information about new players.

Drag a TableViewController to the canvas and place it on the right side of the Players scenario. You can double-click to zoom out the canvas to have more space for operations.

Select the TableViewController and embed it into a navigationController (via the menu "Editor \ EmbedIn \ Navigation Controller ").

Next, please note: Select the + icon and press ctrl to drag it to the newly added navigationController.

Then modify PlayerDetailsViewController. h:

@class PlayerDetailsViewController;  @protocol PlayerDetailsViewControllerDelegate 
 
  - ( void ) playerDetailsViewControllerDidCancel :    ( PlayerDetailsViewController *) controller;- ( void ) playerDetailsViewControllerDidSave :    ( PlayerDetailsViewController *) controller;@end   @interface PlayerDetailsViewController : UITableViewController  @property ( nonatomic, weak ) id 
  
    delegate;  - ( IBAction ) cancel :( id ) sender;- ( IBAction ) done :( id ) sender;  @end
  
 

We have defined a delegate protocol so that when you click Cancel or Done, we can return to the Players window from the AddPlayer window.

Return to the storyboard editor and connect the Cancel button and Done button to the corresponding Action method. The first method is to press ctrl, drag BarButtonItem to ViewController, and select the appropriate action method from the pop-up menu:

Add these two methods to PlayerDetailsViewController. m:

- ( IBAction ) cancel :( id ) sender {        [ self.delegate playerDetailsViewControllerDidCancel : self ] ;} - ( IBAction ) done :( id ) sender {        [ self.delegate playerDetailsViewControllerDidSave : self ] ;}

The two methods correspond to two barbuttons respectively. These methods notify the delegate object of the event triggered. In the end, the delegate object needs to close the window (this is not necessary, but I like to do so. Of course, you can also enable AddPlayer to disable itself before or after notifying the delegate object ).

Note that the delegate method usually places a reference of the caller in the first parameter (or only one parameter) of the method ). This is the PlayerDetailsViewController parameter. This allows the delegate object to know which object sends messages to it.

Do not forget to merge the delegate attributes:

@ Synthesize delegate;

We have defined a delegate protocol for PlayerDetailsViewController. we need to implement it somewhere. Obviously, this should be PlayersViewController. Because it is responsible for rendering the AddPlayer window. Add the following code to PlayerViewController. h:

#import "PlayerDetailsViewController.h"  @interface PlayersViewController : UITableViewController 
 

Add the following at the end of the PlayersViewController. m method:

#pragma mark - PlayerDetailsViewControllerDelegate  - ( void ) playerDetailsViewControllerDidCancel :    ( PlayerDetailsViewController *) controller {        [ self dismissViewControllerAnimated : YES completion : nil ] ;}  - ( void ) playerDetailsViewControllerDidSave :    ( PlayerDetailsViewController *) controller {        [ self dismissViewControllerAnimated : YES completion : nil ] ;}

The AddPlayer window is simply closed. We will do some interesting things later.

The new dismissViewControllerAnimated: completion: method is displayed in iOS5. You can also use the old dismissModalViewControllerAnimated: method. But the new method will more elegantly close ViewController (it can give you a chance to execute additional code after the window is closed ).

There is only one thing left: the Players window needs to tell PlayerDetailsViewController that it will become its delegate. Maybe you want to use the story editor to drag a line between the two? Unfortunately, this does not work. In segue, we still have to write code to pass data to the next ViewController.

Add the following method to PlayersViewController:

- ( void ) prepareForSegue :( UIStoryboardSegue *) segue sender :( id ) sender {        if ([ segue.identifier isEqualToString : @ "AddPlayer" ]) {               UINavigationController * navigationController =             segue.destinationViewController;               PlayerDetailsViewController * playerDetailsViewController = [[ navigationController viewControllers ] objectAtIndex : 0 ] ;               playerDetailsViewController.delegate = self;        }}

The prepareforseue method is triggered when a seue is about to occur. The new ViewController has been loaded with the story version, but it is invisible. we must use this method to pass parameters to it. (Never call the prepareForSegue method by yourself. it is called by UIKit to notify you that a segue is being triggered .)

Note that the target of segue is NavigationController, which is associated with our BarButtonItem. To get the PlayerDetailsViewController instance, we must find it from the viewControllers attribute of NavigationController.

Run the program and press the + icon to close the AddPlayer window. No effect!

That's because we didn't specify the identifier of the segue. In the prepareForSegue method, we checked whether the identifier is AddPlayer. When multiple sews exist in the same ViewController, we recommend that you use identifier to differentiate different sews.

In the story editor, select the rule. Note that the BarButtonItem will be highlighted so that you can clearly know which control triggers the rule.

In the properties Panel, set identifier to AddPlayer.

. This will eliminate some warnings when Xcode adds this class. Run the program to view the effect of the static cell. We didn't write a line of code -- we actually deleted a lot of code.

But we have to write some code. When you want to add the first cell to textField, you may notice that it is slightly inappropriate and there will be extra space around it. Users cannot see the headers and tails of textField, so the keyboard does not pop up when they touch these spaces. This is easy to solve. the code for overwriting the tableView: didSelectRowAtIndexPath method is as follows:

- ( void ) tableView :( UITableView *) tableView    didSelectRowAtIndexPath :( NSIndexPath *) indexPath {        if ( indexPath.section == 0 )               [ self.nameTextField becomeFirstResponder ] ;}

That is, when you touch the first cell, we activate textField focus (there is only one cell in this section, so we only need to check the index of the section ). This will automatically pop up the keyboard. This is only a trivial task, but it can reduce user dissatisfaction.

You should also set the cell's selection style to None in the property panel. otherwise, when the user touches the text box, the entire row will turn blue.

Well, this is the design of the AddPlayer window. Now we need to make it really useful.

The above is the content for getting started with iOS 5 storyboard (3). For more information, see PHP Chinese website (www.php1.cn )!

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.