警告视图例子:请求用户的文本输入

这里有涉及到这么一段代码:
@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];
}


loadView中绑定presentSheet方法

如果按照SDK公开的方法来做的话,肯定想其它方式来处理。晓得了,需要搜集有用的非公开函数,以备用开发。

posted @ 2011-05-25 15:04  西就东城  阅读(629)  评论(0编辑  收藏  举报