iOS5编程--官方例子代码的研究--2.UICatalog-5

来源:互联网 发布:焦作市青峰网络 编辑:程序博客网 时间:2024/04/27 17:58

8.AlertsViewController类

8.1 UIAlertView

这里介绍了两个常用的,获取用户选择的办法,一个是使用类UIActionSheet,另一个是类UIAlertView

这两个类的使用都非常方便,简单。

关于alert view,我仅分析一个

UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"UIAlertView"message:@"Enter a password:"

                                                  delegate:selfcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",nil];

生成一个alert view,如果你对用户的选择表示关心,那么请设置delegate.在响应函数

alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

中,第一个参数是alert view的指针,因为你可能有在一个view controller中显示多个或多次的alert view,关于第二个参数,就是用户的选择,这里没办法取道选择的button的title,但是title是你自己用代码加上去的,所以我们其实不关心title,index的定以上hi这样的。cancel button为0,后面的other button依此为1,2.。。。


alert.alertViewStyle =UIAlertViewStyleSecureTextInput;

上面的这个功能是iOS 5.0中加入的。这里有四种方式,

UIAlertViewStyleDefault = 0,

    UIAlertViewStyleSecureTextInput,

    UIAlertViewStylePlainTextInput,

    UIAlertViewStyleLoginAndPasswordInput

其中第一种是缺省的,即没有任何输入框,第二种是有一个密码输入框,第三种是有一个明文输入框,第四种是二者都有。在alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex,我们可以使用代码

[actionSheettextFieldAtIndex:0].text

取得响应的输入的文本。



    [alert show];

[alertrelease];

上面的两句话,需要关注的是内存管理方面的东西,alert这个变量在alloc的时候有了一块内存,并且计数器为1,那么在调用函数show的时候,计数器会隐含的加1,这样在最后一句话的时候,alert指向的内存的计数器还是1,这里符合规则,你让它加一,你必须负责减一,不是你加的,你就不负责,函数show的对alert所在的内存加一,会在这个对话框消失的时候减一。


8.2 UIActionSheet

关于这个类,我也只是分析一段代码:

// open a dialog with two custom buttons

UIActionSheet *actionSheet = [[UIActionSheetalloc] initWithTitle:@"UIActionSheet <title>"

delegate:selfcancelButtonTitle:nildestructiveButtonTitle:nil

otherButtonTitles:@"Button1",@"Button2", nil];

这里cancel button和 destructive button都可以为nil,或者都不为nil,也可以一个为nil,另一个不是,但是我们需要知道,button index的顺序,如果只有一个,那么这个肯定是0,如果一个也没有,那么下面的other button里面顺序开始,比如这里Button1就是0, Button2就是1,如果两个都有,注意这里和alertview不同,destructive button 为0,cancel button为1,other button从2开始。


actionSheet.actionSheetStyle =UIActionSheetStyleDefault;

actionSheet.destructiveButtonIndex = 1;// make the second button red (destructive)

指定那个button显示为红色,缺省的情况下,如果cancel和 destructive都有,destructive为红色,如果只有一个,或者都没有,那么就没有一个button显示为红色。必须自己指定。



[actionSheetshowInView:self.view];// show from our table view (pops up in the middle of the table)

[actionSheetrelease];




原创粉丝点击