显示标签为“Style”的博文。显示所有博文
显示标签为“Style”的博文。显示所有博文

2011年10月29日星期六

iOS 5 SDK: UIAlertView Text Input and Validation

iOS 5 SDK: UIAlertView Text Input and Validation:
With the release of the iOS 5 SDK, the UIAlertView class has been updated to natively support text input, secure text input, and username/password form fields. This quick tip will demonstrate how to take advantage of these new enhancements!



In a previous full-length tutorial for beginners, I walked the reader through setting up a project with a custom button and background. I’ll use the project created in that tutorial as the starting point for this one.



Introducing Alert View Styles


The layout and form elements of a UIAlertView can now be easily and quickly controlled by setting the alertViewStyle property. Possible options for this property include:

UIAlertViewStylePlainTextInput


This style will add a single text field to the alert view:


UIAlertViewStylePlainTextInput Figure

UIAlertViewStyleSecureTextInput


This style is just like the plain text field, except that all characters entered will be obscured:


UIAlertViewStyleSecureTextInput Figure

UIAlertViewStyleLoginAndPasswordInput


Setting this value for the style property will add both a regular text field input for a username and a secure text field input for a password:


UIAlertViewStyleLoginAndPasswordInput Figure

UIAlertViewStyleDefault


This is the default style we all already know and love:


UIAlertViewStyleDefault Figure

Implementation


Any of the above styles are as easy to implement as setting the alertViewStyle property on your UIAlertView, either with dot notation:

message.alertViewStyle = UIAlertViewStylePlainTextInput;

Or by directly calling the setter method:

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];

That’s it! After you’ve set this property, the alert view display will adjust accordingly.

Of course, displaying text fields on the alert view won’t do much good unless you can also access the value the user enters. This is done with the textFieldAtIndex: method. You simply supply the index of the text field you want to access (0 – 1 in the examples above), and the relevant UITextField object is returned.

For an example of how this might work, consider the -alertView:clickedButtonAtIndex: delegate method we’ve already implemented in the attached demo project. If we change the alert view style to UIAlertViewStyleLoginAndPasswordInput and change the default button title to “Login”, then we could access the values entered by the user in the delegate method like so:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

if([title isEqualToString:@"Login"])
{
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];

NSLog(@"Username: %@\nPassword: %@", username.text, password.text);
}
}



Potential Pitfalls


There a few ways that using the new alert styles might backfire.

Too Many Buttons


If you add as few as three buttons to the UIAlertView, the layout of each new style will become skewed.


UIAlertView Figure

All of the new styles can handle 2 buttons comfortably, but the default style used in the past can handle up to 6 when the title and message is removed:


UIAlertView Figure

Too Much Text


The UIAlertViewStyleLoginAndPasswordInput style can’t handle overflow text gracefully the way the other styles can:


UIAlertViewStyleLoginAndPasswordInput Figure

Even entering just a brief message like “You must enter both a username and password.” was enough to ruin the aesthetic.



Validating Input


In addition to the button styles discussed above, the iOS 5 SDK also introduced a new UIAlertViewDelegate method, -alertViewShouldEnableFirstOtherButton:. This method is called both when the alert view is first displayed and also each time the user types a character into one of the text fields, making it very easy to perform basic input validation prior to accepting a user’s value.

Consider the following example:

- (IBAction)showMessage:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"What is your phone number?"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];

[message show];
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 10 )
{
return YES;
}
else
{
return NO;
}
}

The code above will only allow the user to press the “Continue” button after they have entered at least 10 characters into the form field. I’m sure you’ll want to make your own validation more robust, but this basic example shows just how useful this new delegate method can be.

2011年9月26日星期一

Objective-C Comment Styles

Objective-C Comment Styles:


A few weeks ago I wrote about Objective-C Indentation Styles. A reader (thanks Joe) commented and suggested a similar post that covered comment formats. That blog post follows…

Interface File Comments

When I create an interface file, here is the basic layout of my comments:


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.h
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this object would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

@interface AboutViewController : UIViewController
{
 UIButton          *backButton;  // Comment here
 NSMutableArray    *queueArray;  // Comment here
 NSMutableArray    *bucketArray;  // Comment here

 //-------------------------------------------------------
 // To describe something in more detail, it would go here
 // searchResultsCount varaible will hold ....
 // questionsResultsCount is calculated by ...
 //-------------------------------------------------------
 int               searchResultsCount;     // Comment here
 int      questionsResultsCount;  // Comment here

}

@end




I’m not sure when it happened, somewhere on the way I moved away from the traditional comment block shown below, I don’t use this for the header or inside the primary code sections. I must of tired of the look and made a small tweak to us ~ and – characters.


/**********************************************************
* To describe something in more detail, it would go here
* searchResultsCount varaible will hold ....
*  questionsResultsCount is calculated by ...
**********************************************************/


Implementation File Comments

At the top of the implementation file, my header looks the same as the interface file. If there is a private interface (see the post Private Methods for more information), I follow the same layout:


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.m
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this class would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#import "AboutViewController.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@interface AboutViewController(private)
- (BOOL) processSearch:(NSString *)string;
@end


For methods in the class, my comment format looks as follows:


/*---------------------------------------------------------------------------
* Manage button precess events
*--------------------------------------------------------------------------*/
- (void)buttonPressed:(UIButton *)button
{
 // For one line comment
 if (button == backButton)
 {
 }

 //-------------------------------------------------------
 // If more than one line of comments is needed, 
 // I use this format...
 //-------------------------------------------------------
 ...

}


Here is a template of what my code looks like with #pragma marks included:


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*  AboutViewController.m
*
*  Created by John on 6/10/11.
*  Copyright 2011 iOSDeveloperTips.com. All rights reserved.
*
*  Long comments describing this object would go here...
*  ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#import "AboutViewController.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@interface AboutViewController(private)
- (BOOL) processSearch:(NSString *)string;
@end

@implementation AboutViewController

#pragma mark - Private Methods

/*---------------------------------------------------------------------------
* This method....
*--------------------------------------------------------------------------*/
- (BOOL) processSearch:(NSString *)string
{
 ... 
}

#pragma mark - Initialization

/*---------------------------------------------------------------------------
* Initialization of ...
* More comments here...
*--------------------------------------------------------------------------*/
- (id) init
{

}

#pragma mark - Event Management

/*---------------------------------------------------------------------------
* Manage button precess events
*--------------------------------------------------------------------------*/
- (void)buttonPressed:(UIButton *)button
{
 // For one line comment
 if (button == backButton)
 {
 }

 //-------------------------------------------------------
 // If more than one line of comments is needed, 
 // I use this format...
 //-------------------------------------------------------
 ...

} 

#pragma mark - Cleanup

/*---------------------------------------------------------------------------
* Cleanup code
*--------------------------------------------------------------------------*/
- (void)dealloc
{
 ...

 [super dealloc];
}

@end


The beauty of using #pragma mark is that within Xcode you can get a nice breakdown of the organization of your code:



You can view this dialog from within Xcode by clicking on the class status bar in the boxed area shown below:



Posting a Comment with Code

Please feel free to post code examples of your comment style. If you would like have your code color highlighted as shown above, using the following format:

<pre lang=”objc”>

your code here

</pre>