iOS中警告视图的简单应用

移动开发 iOS
本文介绍了iOS中警告视图的简单应用,具体讲解了如何使用UIAlertView类显示警告信息给用户看,希望对大家有所帮助。

创建一个警告,具体代码只有如下:

  1. - (void) presentSheet 
  2. UIAlertView *baseAlert = [[UIAlertView alloc] 
  3. initWithTitle:@"Alert" message:@"" 
  4. delegate:self cancelButtonTitle:nil 
  5. otherButtonTitles:@"OK", nil]; 
  6. [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
参看代码:

  1. - (void) performDismiss: (NSTimer *)timer 
  2. [baseAlert dismissWithClickedButtonIndex:0 animated:NO]; 
  3. [baseAlert release]; 
  4. baseAlert = NULL; 
  5. - (void) presentSheet 
  6. baseAlert = [[UIAlertView alloc] 
  7. initWithTitle:@"Alert" message:@"\nMessage to user with asynchronous information" 
  8. delegate:self cancelButtonTitle:nil 
  9. otherButtonTitles: nil];//注意cancelButtonTitle和otherButtonTitles都nil 
  10. [NSTimer scheduledTimerWithTimeInterval:3.0f 
  11. target:self 
  12. selector: @selector(performDismiss:) 
  13. userInfo:nil repeats:NO];//注意repeats:NO 
  14. [baseAlert show]; 

 

责任编辑:闫佳明 来源: cnblogs
相关推荐

2013-06-14 13:31:30

iOS开发移动开发警告视图

2021-07-28 10:07:19

iOS 15苹果地图天气

2011-09-02 19:12:59

IOS应用Sqlite数据库

2012-05-13 13:15:54

IOS

2012-05-14 17:10:50

iOS

2011-12-28 15:11:09

iOS推荐

2010-03-09 10:49:35

python简单应用

2011-01-27 09:20:11

Samba应用案例

2011-06-27 15:08:18

QML 视图

2011-08-17 15:10:21

iPhone开发Web视图

2011-07-22 13:23:56

IOS UI ScrollView

2014-04-23 13:30:23

类簇iOS开发

2013-03-29 11:06:24

iOS开发滚动视图UIScrol

2011-07-08 14:51:34

iPhone 视图

2009-11-17 16:47:09

Oracle物化视图日

2015-01-20 17:15:55

iOS源码滚动视图

2010-05-18 14:21:35

MySQL视图

2015-05-13 09:15:50

应用程序开发PaaSAWS

2015-12-30 14:16:05

iOS动画视图渲染

2015-12-23 09:16:33

ios动画渲染机制
点赞
收藏

51CTO技术栈公众号