text stringlengths 0 13.4k |
|---|
108 |
Tutorial: Add Data |
Add New Items |
3. |
In the method, see whether the Done button was tapped. |
If it wasn’t, instead of saving the item, you want the method to return without doing anything else. |
if (sender != self.doneButton) return; |
4. |
See whether there’s text in the text field. |
if (self.textField.text.length > 0) { |
} |
5. |
If there’s text, create a new item and give it the name of the text in the text field. Also, ensure that the |
completed state is set to NO. |
self.toDoItem = [[XYZToDoItem alloc] init]; |
self.toDoItem.itemName = self.textField.text; |
self.toDoItem.completed = NO; |
If there isn’t text, you don’t want to save the item, so you won’t do anything else. |
Your prepareForSegue: method should look 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’ve created a new item, you need to pass the item back to XYZToDoListViewController so |
that it can add the item to the to-do list. To accomplish this, you need to revisit the unwindToList: method |
that you wrote in the second tutorial. This method gets called when the XYZAddToDoItemViewController |
scene closes, which happens when the user taps either the Cancel or the Done button. |
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved. |
109 |
Tutorial: Add Data |
Add New Items |
The unwindToList: method takes a segue as a parameter, like all methods that are used as targets for an |
unwind segue. The segue parameter is the segue that unwinds from XYZAddToDoItemViewController |
back to XYZToDoListViewController. Because a segue is a transition between two view controllers, it is |
aware of its source view controller—XYZAddToDoItemViewController. By asking the segue object for its |
source view controller, you can access any data stored in the source view controller in the unwindToList: |
method. In this case, you want to access toDoItem. If it’s nil, the item was never created—either the text |
field had no text or the user tapped the Cancel button. If there’s a value for toDoItem, you retrieve the item, |
add it to your toDoItems array, and display it in the to-do list by reloading the data in the table view. |
To store and display the new item |
1. |
In the project navigator, select XYZToDoListViewController.m. |
2. Add an import declaration to the XYZAddToDoItemViewController class above the @interface line. |
#import "XYZAddToDoItemViewController.h" |
3. |
4. |
Find the unwindToList: method you added in the second tutorial. |
In this method, retrieve the source view controller—the controller you’re unwinding from, |
XYZAddToDoItemViewController. |
XYZAddToDoItemViewController *source = [segue sourceViewController]; |
5. |
Retrieve the controller’s to-do item. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.