AlertView 之 textFieldView

@interface UIAlertView (extended)
- (UITextField *) textFieldAtIndex: (int) index;
- (void) addTextFieldWithValue: (NSString *) value label: (NSString *) label;
@end
 
 
前面有一篇blog也涉及到(extended),没有理解到具体。
 
这里解答下:
先以上述代码来说,假定UIAlertView的头文件和实现文件分别是UIAlertView.h和UIAlertView.m(之所以假定,因为你只找到UIAlertView.h文件,找不到UIAlertView.m文件,这个实现文件肯定被编译,不会公开的)
 
代码中的方法textFieldAtIndex和addTextFieldWithValue,在头文件找不到,而在实现文件中。至于是不是Category实现就不可而知。
 
这里需要把隐藏的方法声明一下
 
好了,回到正题来,后续代码很简单,看下:
 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    printf("User Pressed Button %d\n", buttonIndex + 1);
    printf("Text Field 1: %s\n", [[[alertView textFieldAtIndex:0] text] cStringUsingEncoding:1]);
    printf("Text Field 2: %s\n", [[[alertView textFieldAtIndex:1] text] cStringUsingEncoding:1]);   
    [alertView release];
}
 
- (void) presentSheet
{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Enter Information"
                          message:@"Specify the Name and URL"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"OK", nil];
    [alert addTextFieldWithValue:@"" label:@"Enter Name"];
    [alert addTextFieldWithValue:@"http://" label:@"Enter URL"];
    
    // Name field
    UITextField *tf = [alert textFieldAtIndex:0];
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = UIKeyboardTypeAlphabet;
    tf.keyboardAppearance = UIKeyboardAppearanceAlert;
    tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
    tf.autocorrectionType = UITextAutocorrectionTypeNo;
    
    // URL field
    tf = [alert textFieldAtIndex:1];
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = UIKeyboardTypeURL;
    tf.keyboardAppearance = UIKeyboardAppearanceAlert;
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tf.autocorrectionType = UITextAutocorrectionTypeNo;
    
    [alert show];
}

你可能感兴趣的:(AlertView 之 textFieldView)