创建一个警告,具体代码只有如下:
- (void) presentSheet
{
UIAlertView *baseAlert = [[UIAlertView alloc]
initWithTitle:@"Alert" message:@""
delegate:self cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[baseAlert show];
}
类学习
UIAlertView类
继承UIView
Use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).
使用UIAlertView类显示警告信息给用户看。警告视图函数类似但不同于从动作表上的呈现(UIActionSheet实例)
属性:
delegate
title
message
visible
//这里可以看出在init方法调用的参数部分可以由属性来设置
cancelButtonIndex:-1表示未设置.
firstOtherButtonIndex:此属性只读
numberOfButtons:按钮个数,只读
方法:
– initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
– show
– addButtonWithTitle:通过所给标题添加按钮
– buttonTitleAtIndex:返回指定索引下的按钮标题
– dismissWithClickedButtonIndex:animated:清除接收器,动画可选
针对UIAlertView视图类如何响应按钮触发?
这里要用到
UIAlertViewDelegate Protocol
此协议接口定义UIAlertView对象委托需要执行的方法
Responding to Actions
– alertView:clickedButtonAtIndex:当用户在警告视图点击按钮时发送给委托处理并响应
Customizing Behavior
– willPresentAlertView:警告视图呈现给用户前发送给委托
– didPresentAlertView:警告视图呈现给用户后发送给委托
– alertView:willDismissWithButtonIndex:在警告视图清除前发送给委托
– alertView:didDismissWithButtonIndex:在警告视图从屏幕离开后发送给委托
Canceling
– alertViewCancel:在警告视图中止前发送给委托
整体来说,警告视图类的方法和触发事件都非常简单
在写触发事件时需要继承<UIAlertViewDelegate>协议接口
/************************************************************/
后续一例子:自动计时无按钮警告
这个例子咋看是一个新的东西,仔细阅读下代码,就是使用NSTimer和UIAlertView
注意两个地方:
1、创建警告视图的时候,不要添加Button
2、Timer关闭警告视图的时候,设置Repeat参数=No
参看代码:
- (void) performDismiss: (NSTimer *)timer
{
[baseAlert dismissWithClickedButtonIndex:0 animated:NO];
[baseAlert release];
baseAlert = NULL;
}
- (void) presentSheet
{
baseAlert = [[UIAlertView alloc]
initWithTitle:@"Alert" message:@"\nMessage to user with asynchronous information"
delegate:self cancelButtonTitle:nil
otherButtonTitles: nil];//注意cancelButtonTitle和otherButtonTitles都nil
[NSTimer scheduledTimerWithTimeInterval:3.0f
target:self
selector: @selector(performDismiss:)
userInfo:nil repeats:NO];//注意repeats:NO
[baseAlert show];
}
故:多思多实践,多阅读多动手~