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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值