提示(警告)视图的简单应用

Pdf版P101和103涉及的例子进行学习

创建一个警告,具体代码只有如下:
 
  
- ( 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];
}

故:多思多实践,多阅读多动手~


转载于:https://www.cnblogs.com/GoGoagg/archive/2011/05/24/2055382.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值