Graphics Programs Reference
In-Depth Information
data to the next
UIViewController
(like you just did) is a clean and efficient way of
performing this task.
Build and run your application. Create a new item and select that row in the
UIT-
ableView
. The view that appears will contain the information for the selected
BNRItem
. While you can edit this data, the
UITableView
won't reflect those changes
when you return to it. To fix this problem, you need to implement code to update the prop-
erties of the
BNRItem
being edited. In the next section, we'll see when to do this.
Appearing and disappearing views
Whenever a
UINavigationController
is about to swap views, it sends out two
messages:
viewWillDisappear:
and
viewWillAppear:
. The
UIViewCon-
troller
that is about to be popped off the stack is sent the message
viewWillDis-
appear:
. The
UIViewController
that will then be on top of the stack is sent
viewWillAppear:
.
When a
DetailViewController
is popped off the stack, you will set the properties
of its
item
to the contents of the
UITextField
s. When implementing these methods
for views appearing and disappearing, it is important to call the superclass's implementa-
tion - it has some work to do as well. In
DetailViewController.m
, implement
viewWillDisappear:
.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Clear first responder
[[self view] endEditing:YES];
// "Save" changes to item
[item setItemName:[nameField text]];
[item setSerialNumber:[serialNumberField text]];
[item setValueInDollars:[[valueField text] intValue]];
}
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 should be forced into retirement. Some first responders might refuse to
resign, and passing
YES
ignores that refusal.)