Database Reference
In-Depth Information
The first parameter of this method gives us the
UITextField
that called it. We start off by calling our
updateDefaultForTextField
method and passing it the
textField
object. We then use the
isEqual
method on
NSObject:
to determine if we are working with the
_txtDisplayName
object.
If we are, then we call the method
becomeFirstResponder
on our
_txtFavoriteNumber
object. This
causes the focus to change to our
_txtFavoriteNumber
object and displays the keyboard if it is not
already being displayed. The else portion of the statement is used if this method was called
by
_txtFavoriteNumber
. If that is the case, then we call the
resignFirstResponder
. This causes
the focus to move from the text field and causes the keyboard to hide.
We are almost done, but we haven't handled the Bar Button Item that we will use to leave the
view. To handle the Close button press, we will add two methods. The first method simply calls our
updateDefaultTextField:
method on each of our text fields to handle our saving of the data. We call
this method
updateAllTextFields
.
-(void)updateAllDefaults {
[self updateDefaultForTextField:_txtDisplayName];
[self updateDefaultForTextField:_txtFavoriteNumber];
}
Now we add a method called
btnClose:
that calls our
updateAllDefaults
method and also
dismisses our view controller, which returns us to our
MainViewController
. We add another pragma
mark above it for good organization.
#pragma mark - Button Methods
-(IBAction)btnClosePressed:(id)sender {
[self updateAllDefaults];
[self dismissViewControllerAnimated:YES completion:nil];
}
The only thing new here is that we are calling an instance method
dismissViewControllerAnimated
:completion:
against our
SettingsViewController
. This method has two parameters: a
BOOL
and a
code block. We want to set the first parameter to YES so that we get the nice reverse animation that
is used when we move to this view. Set the second parameter to nil because we don't want to do
anything once the view has been dismissed.
Back to the Storyboard
The last thing we need to do is make some connections in our storyboard and we will be ready to
test the app. Go ahead and select
Main.storyboard
in the Project Navigator.
Select
MainViewController
in the Document Outline.
1.
2.
Expand the View so you can see the Label and the Button.
Ctrl-drag from the Main View Controller to the Label and select
lblWelcome
.
3.
Select the
SettingsViewController
in the Document Outline.
4.
5.
Show the Identity Inspector in the Utilities pane.
Search WWH ::
Custom Search