Add ios7 data 2 -- Start Developing iOS Apps Today -- develop IOS (IOS7) from Today

Source: Internet
Author: User

It is not good to mark projects as completed to-do lists. if the projects are completed, you will never be able to mark them. Now, you will add this support. In a simple interface, when the status switch is complete, the user clicks the cell and displays the completed projects with a check mark next to them. Fortunately, the table view is equipped with some built-in behaviors that you can fully utilize to implement this simple interface. Specifically, the table view notifies the user to delegate a click on a cell. Therefore, this task is to write the code to respond to the user in the to-do list. Xcode has obtained XYZToDoListViewController when you configure its table view delegate in the script. All you need to do is implement the implementation code tableView: didSelectRowAtIndexPath: Delegate method to respond to the user's faucet, and update your to-do list item as appropriate.
When a cell is selected, call the implementation code tableView: didSelectRowAtIndexPath: Delegate method to see how to handle the selection. In this method, you will write code to update the completion status of the to-do project.
Sign project to be completed or not completed
1. In the project navigator, select XYZToDoListViewController. m.
2. Add the following statement to the end of the file, slightly higher than the @ end line:

#pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ }
Try to input, not just copy and paste the second line. You will find that code completion is one of Xcode's great time-saving functions. In Xcode, a list of potential projects is provided. Scroll through the list until you find what you want and press Enter. Insert the entire line to you in Xcode.
3. the header you want to deal with, but you may not select this cell. After the following code is added, the selected cell is canceled immediately:
[tableView deselectRowAtIndexPath:indexPath animated:NO];

4. Find the corresponding XYZToDoItem in your toDoItems array.
XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
5. Switch the completion status of the tapped project.
tappedItem.completed = !tappedItem.completed;
6. Tell the table view to reload the row whose data has just been updated.
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
Your tableView: didSelectRowAtIndexPath: method should be as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:NO];    XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];    tappedItem.completed = !tappedItem.completed;    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];}

Key Point: Run your application. The list of new projects you added to loadInitialData is visible in the table view. However, when you click a project, nothing seems to happen. Why not?
The reason is that you have not configured the table view cell to display the project completion status. To do this, you need to return to the implementation code tableView: cellForRowAtIndexPath: The Metric displayed after a project is completed.
One way to indicate that the project has been completed is to mark a check item. Fortunately, table View cells can be accessories in cells on the right. By default, no accessories are available. However, you can change cells to display different accessories. One of them is a check mark. All you need to do is set based on the completion status of the to-do project.
To display the completion status of a project
1. Go to tableView: cellForRowAtIndexPath: method.
2. Add the following code at the bottom to set the line of the text label of the Cell:
if (toDoItem.completed) {    cell.accessoryType = UITableViewCellAccessoryCheckmark;} else {    cell.accessoryType = UITableViewCellAccessoryNone;}

TableView: cellForRowAtIndexPath: the method should be as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"ListPrototypeCell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];    cell.textLabel.text = toDoItem.itemName;    if (toDoItem.completed) {        cell.accessoryType = UITableViewCellAccessoryCheckmark;    } else {        cell.accessoryType = UITableViewCellAccessoryNone;    }    return cell;}

Key Point: Run your application. The list of new projects you added to loadInitialData is visible in the table view. When you click a project, a check mark should appear next to it. If you click the same project again, the check mark disappears.

The last step to add a project to create a to-do list application is to add a project. When you enter the project name in the text field XYZAddToDoItemViewController scenario and Done button, you want the View Controller to create a new list item, return to XYZToDoListViewController and display it in the to-do list.
First, you need a list item for configuration. Just like the table view, the View Controller Interface is connected to a reasonable location of the model. Give XYZAddToDoItemViewController an attribute to save the new to-do list.

Add XYZToDoItem to the XYZAddToDoItemViewController class
1. In the project navigator, select XYZToDoListViewController. h.
Because you need to access the list item from your table View Controller, it is important to make this public property. This is why XYZAddToDoItemViewController. h In the interface file replaces XYZAddToDoItemViewController. m declaration in the implementation file.
2. Add the @ interface line XYZToDoItem class above the import declaration.
#import "XYZToDoItem.h"
3. Add toDoItem to the interface
@interface XYZAddToDoItemViewController : UIViewController @property XYZToDoItem *toDoItem; @end

To obtain the name of a new project, the view controller needs to access the text field in which the user enters the name. To do this, create a connection to the Storyboard class in the text field from XYZAddToDoItemViewController.
This text field is connected to your view Controller

1. In the Outline View, select the XYZAddToDoItemViewController object.
2. Click the toolbar in the window to open the Assistant button in the upper-right corner of assistant editing.
Memory + o6zNo9a5zc + memory/2 s/fwre1xM/fwrfjz6gjpgjypgokpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140125/2014012509102548.jpg" width = "500" height = "500" alt = "\">
5. In the displayed dialog box, for the name and TextField type.
Leave other options because they. Your dialog box should look like this:

6. Click Connect.
Add the necessary code in Xcode to store the pointer of XYZAddToDoItemViewController. m to the text field and configure the script to set the connection.

In addition, you need to know when to create the project. You want to create a project where only the completion button is eavesdropped. To do this, add the finish button as an exit.

Connect the finish button to your view Controller
1. In your script, open assistant editing and set XYZAddToDoItemViewController. m in the rightmost window.
2. Select the Done button in your script.
3. Press the Control key and drag the finish button to display the code in the editor on the right. Stop dragging slightly behind the lines in XYZAddToDoItemViewController. m's TextField attribute.
4. In the displayed dialog box, type doneButton in the name.
Leave other options because they. Your dialog box should look like this:

5. Click Connect.
You now have a method to identify the completion button. You need to know when the Done button is eavesdropped when creating a project.
When the user clicks the finish button, it starts to show the list of to-do items of the seue, which is the interface configured in your second tutorial. Prior to executing a segr operation, the system provides a chance to participate in the preparation of the View Controller by calling prepareForSegue. This is exactly the point where you want to check whether the user has shot the Done button. If so, create a new to-do list. You can check which buttons are mined. If it is finished, create the project.

Create a project after attacking the Done button
1. Select XYZAddToDoItemViewController. m in the project navigator.
2. Add prepareForSegue: The following @ line implementation method:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{}
3. Check whether the Done button is eavesdropped.
Instead of saving the project, if not, you want to avoid any other return methods.
if (sender != self.doneButton) return;

4. check whether there is any text in the text field.
if (self.textField.text.length > 0) {}
5. If there is text, create a new project and give it the name of the text in the text field. In addition, make sure that the completion status is set to NO.
self.toDoItem = [[XYZToDoItem alloc] init];self.toDoItem.itemName = self.textField.text;self.toDoItem.completed = NO;

If there is no text, you do not want to save the project, so you will not do anything else.
Your prepareForSegue: the method should be like this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if (sender != self.doneButton) return;    if (self.textField.text.length > 0) {        self.toDoItem = [[XYZToDoItem alloc] init];        self.toDoItem.itemName = self.textField.text;        self.toDoItem.completed = NO;    }}

Now that you have created a new project, you need to return to XYZToDoListViewController through the project so that it can add the project to the to-do list. To do this, you need to review the unwindToList: method you wrote in the second tutorial. This method is called XYZAddToDoItemViewController. When a user clicks the cancel or complete button.
The unwindToList: The method accepts a segue as the parameter, and serves as all the methods for expanding the SEGUE of the target. The Segui parameter is used to return XYZToDoListViewController's seue from the open volume XYZAddToDoItemViewController. Because segue is the transition between two view controllers, it knows its source code View Controller XYZAddToDoItemViewController. By querying its source View Controller, you can access any data stored in the unwindToList source View Controller: method. In this case, the TodoItem you want to access. If it is nil, the project has not been created, whether the text field does not contain any text or the user clicks the cancel button. If there is a specific TodoItem value, retrieve the item, add it to your toDoItems array, and reload the data in the table view to display it in the to-do list.

New projects for storage and display

1. In the project navigator, select XYZToDoListViewController. m.
2. Add the XYZAddToDoItemViewController class of the @ interface line above the import declaration.
#import "XYZAddToDoItemViewController.h"
3. Find unwindToList: add the method in the second tutorial.
4. In this method, retrieve the controller from which you removed the source view controller from XYZAddToDoItemViewController.
XYZAddToDoItemViewController *source = [segue sourceViewController];

5. Get the to-do list of the controller.
XYZToDoItem *item = source.toDoItem;
This is the project created when the completion button is eavesdropped.
6. Check whether the project exists.
if (item != nil) {}

If it is nil, either cancel the button to close the screen or text fields without text, so you do not want to save the project.
If it does exist, add the project to your toDoItems array.
[self.toDoItems addObject:item];

7. Refresh the data in your table.
Because the table view does not track its data and its data source, in this case, your table View Controller will display new data when notifying the table view.
[self.tableView reloadData];

Your unwindToList: method should be like this:
- (IBAction)unwindToList:(UIStoryboardSegue *)segue{    XYZAddToDoItemViewController *source = [segue sourceViewController];    XYZToDoItem *item = source.toDoItem;    if (item != nil) {        [self.toDoItems addObject:item];        [self.tableView reloadData];    }}

Key Point: Run your application. Now, when you click the Add button (+) and create a new project, you should see it in your to-do list. Congratulations! You have created an application that requires input from the user. It is stored in an object and transmitted between two view controllers. This is the foundation of Storyboard-based applications for data between mobile scenarios.
Review your tour with this introduction about developing iOS apps almost. The last section provides you with more information about how to discover the documents you are taking, and provides the next step you may need to learn how to create more advanced applications.

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.