Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.67 MB, 89 trang )
Figure 2-1. An alert view telling the user that she needs an active internet connection
The best way to initialize an alert view is, of course, through using its designated initializer:
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView show];
2.1 Displaying Alerts with UIAlertView | 111
When this alert view is displayed to the user, she will see something similar to that
shown in Figure 2-2:
Figure 2-2. A simple alert view displayed to the user
In order to display an alert view to the user, we use the alert view's show method. Let's
have a look at the description for each one of the parameters that we passed to the
initializer of the alert view:
title
This is the string that the alert view will display on the top when it is shown to the
user. This string is Title in Figure 2-2.
112 | Chapter 2: Implementing Controllers and Views
message
This is the actual message that gets displayed to the user. In Figure 2-2, this message
is set to Message.
delegate
This is the optional delegate object that we pass to the alert view. This object will
then get notified whenever the alert's state changes, for instance, when the user
taps on a button on the alert view. The object passed to this parameter must conform to the UIAlertViewDelegate protocol.
cancelButtonTitle
This is a string that will get assigned to the cancel button on an alert view. An alert
view that has a cancel button usually asks the user for an action. If the user isn't
comfortable with performing that action, he or she will press the cancel button.
This button's title does not necessarily have to say Cancel. It is up to you to specify
a title for this button. This parameter is optional.
otherButtonTitles
Any other buttons that you want to have apear on the alert view, you pass their
titles here. Separate the titles with a comma and make sure you terminate the list
of titles with a nil or as it's called, a sentinel. This parameter is optional.
It is possible to create an alert view without any buttons. An alert view
without a button cannot be dismissed by the user so you, as the programmer, need to make sure this alert view will get dismissed automatically, for instance, 3 seconds after it is displayed. An alert view without
any buttons that does not dismiss itself automatically gives a really poor
user experience and not only will your app get low ratings on the App
Store for blocking the UI from user-access, chances are that your app
will get rejected by Apple.
Alert views can take various styles. The UIAlertView class has a property called alert
ViewStyle of type UIAlertViewStyle:
typedef enum {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;
Here is what each of these styles will do:
UIAlertViewStyleDefault
This is the default style of an alert view, as we saw in Figure 2-2.
UIAlertViewStyleSecureTextInput
With this style, the alert view will contain a secure text field for secure text entry.
For instance, if you are asking the user for her online banking credentials, then you
can potentially use this style of alert view.
2.1 Displaying Alerts with UIAlertView | 113
UIAlertViewStylePlainTextInput
Under this style, the alert view will display a non-secure text field to the user. This
style is great if you simply want to ask the user for plain text entry, such as the
user's phone number.
UIAlertViewStyleLoginAndPasswordInput
With this style, the alert view will display two text fields. One non-secure for username and another, a secure text field, for password.
Once you decide that you need to get notified when the user interacts with the alert
view, it is time that you specify a delegate object to your alert view. This delegate must
conform to the UIAlertViewDelegate protocol. The most important method defined in
this protocol is the alertView:clickedButtonAtIndex: method which gets called as soon
as the user taps on one of the buttons in the alert view. The button index is passed to
you through the clickedButtonAtIndex parameter.
Let's, as an example, display an alert view to the user and ask whether she could be
bothered to rate our app in the App Store. We will display two buttons on our alert
view: Yes and No. In our alert view delegate, we will detect which button she tapped
on and will take action accordingly.
Let's first implement two very simple methods that return the title of our two buttons:
- (NSString *) yesButtonTitle{
return @"Yes";
}
- (NSString *) noButtonTitle{
return @"No";
}
Now we need to make sure that we are conforming to the UIAlertViewDelegate protocol
in our view controller:
#import
@interface Displaying_Alerts_with_UIAlertViewViewController
: UIViewController
@end
All good. The next step would be to create and display our alert view to the user:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.view.backgroundColor = [UIColor whiteColor];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Rating"
message:@"Can you please rate our app?"
delegate:self
cancelButtonTitle:[self noButtonTitle]
otherButtonTitles:[self yesButtonTitle], nil];
114 | Chapter 2: Implementing Controllers and Views
[alertView show];
}
So now, our alert view will look similar to that shown in Figure 2-3:
Figure 2-3. An alert view with Yes and No buttons
Now we need a way to know whether the user selected the Yes or the No option in our
alert view. For this, we will need to implement the alertView:clickedButtonAtIndex:
method of our alert view delegate:
2.1 Displaying Alerts with UIAlertView | 115
- (void)
alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:[self yesButtonTitle]]){
NSLog(@"User pressed the Yes button.");
}
else if ([buttonTitle isEqualToString:[self noButtonTitle]]){
NSLog(@"User pressed the No button.");
}
}
As you can see, we are using the buttonTitleAtIndex: method of UIAlertView. We pass
the zero-based index of a button inside that alert view to this method and we will get
the string that represents the title of that button, if any. Using this method, we can
determing which button the user has tapped on. The index of that button will be passed
to us as the buttonIndex parameter of the alertView:clickedButtonAtIndex: method
but if you need the title of that button, you will then need to use the buttonTitleAtIn
dex: method of UIAlertView. That is it; job done!
You can also use an alert view to ask the user for text entry, for instance, if you would
like to ask the user for their credit card number or their address. For this, as mentioned
before, we need to use the UIAlertViewStylePlainTextInput alert view style. Here is an
example:
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Credit Card Number"
message:@"Please enter your credit card number:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
/* Display a numerical keypad for this text field */
UITextField *textField = [alertView textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
[alertView show];
}
If we run our app on the simulator now, we will get result similar to that shown here:
116 | Chapter 2: Implementing Controllers and Views
Figure 2-4. An alert view with plain text input
We did change the alert view's style to UIAlertViewStylePlainTextInput in this code
but we did something else as well. We retrieved the reference to the first and the only
text field that we knew we would have on the alert view after changing its style to
UIAlertViewStylePlainTextInput, and we used that text field's reference to change the
keyboard type of the text field. For more information about text fields, please refer to
Recipe 2.14.
2.1 Displaying Alerts with UIAlertView | 117
In addition to a plain text entry, you can ask the user for secure text. You would normally use this if the text that the user is entering needs to be secured. For instance, if
you are asking the user to enter her password. Here is an exmaple:
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Password"
message:@"Please enter your password:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[alertView show];
}
118 | Chapter 2: Implementing Controllers and Views
Figure 2-5. Secure text entry in an alert view
And as you can see, the style that we've chosen is UIAlertViewStyleSecureTextInput.
This style is very similar to the UIAlertViewStylePlainTextInput style except that the
text field is set to secure the entered text.
The next style which is quite useful displays two text fields. One for username and the
other one for password. So, one plain text entry and the other one secure:
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIAlertView *alertView = [[UIAlertView alloc]
2.1 Displaying Alerts with UIAlertView | 119
initWithTitle:@"Password"
message:@"Please enter your credentials:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[alertView show];
}
And the results will look similar to that shown in Figure 2-6:
Figure 2-6. Login and Password style of alert view
120 | Chapter 2: Implementing Controllers and Views
See Also
XXX
2.2 Creating and Using Switches with UISwitch
Problem
You would like to, through your UI, give your users the ability to turn an option on or
off.
Solution
Use the UISwitch class.
Discussion
The UISwitch class provides an On/Off user control like the ones shown in Figure 2-7 for Auto-Capitalization, Auto-Correction and so on...
2.2 Creating and Using Switches with UISwitch | 121