text
stringlengths
0
13.4k
To connect the text field to your view controller
1.
In the outline view, select the XYZAddToDoItemViewController object.
2. Click the Assistant button in the upper right of the window鈥檚 toolbar to open the assistant editor.
The editor on the right should appear with XYZAddToDoItemViewController.m displayed. If it isn鈥檛
displayed, click the filename in the editor on the right and choose XYZAddToDoItemViewController.m.
The assistant editor allows you to have two files open at once, making it possible to perform operations
between them鈥攆or example, tying a property in your source file with an object in your interface.
3.
Select the text field in your storyboard.
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved.
106
Tutorial: Add Data
Add New Items
4. Control-drag from the text field on your canvas to the code display in the editor on the right, stopping
the drag at the line just below the @interface line in XYZAddToDoItemViewController.m.
5.
In the dialog that appears, for Name, type textField.
Leave the rest of the options as they are. Your dialog should look like this:
6. Click Connect.
Xcode adds the necessary code to XYZAddToDoItemViewController.m to store a pointer to the text
field and configures the storyboard to set up that connection.
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved.
107
Tutorial: Add Data
Add New Items
Additionally, you need to know when to create the item. You want to create the item only if the Done button
was tapped. To do this, add the Done button as an outlet.
To connect the Done button to your view controller
1.
2.
3.
In your storyboard, open the assistant editor, and set the rightmost window to
XYZAddToDoItemViewController.m.
Select the Done button in your storyboard.
Control-drag from the Done button on your canvas to the code display in the editor on the right, stopping
the drag at the line just below your textField property in XYZAddToDoItemViewController.m.
4.
In the dialog that appears, for Name, type doneButton.
Leave the rest of the options as they are. Your dialog should look like this:
5.
Click Connect.
You now have a way to identify the Done button. Because you want to create an item when the Done button
is tapped, you need to know when that happens.
When the user taps the Done button, it kicks off an unwind segue back to the to-do list鈥攖hat鈥檚 the interface
you configured in the second tutorial. Before a segue executes, the system gives the view controller involved
a chance to prepare by calling prepareForSegue:. This is exactly the point at which you want to check to
see whether the user tapped the Done button, and if so, create a new to-do item. You can check which one
of the buttons got tapped, and if it was the Done button, create the item.
To create an item after tapping the Done button
1.
Select XYZAddToDoItemViewController.m in the project navigator.
2. Add the prepareForSegue: method below the @implementation line:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved.