iPhone开发中了解UIALertView和UIActionSheet应用

移动开发 iOS
本文介绍的是iPhone开发中了解UIALertView和UIActionSheet应用,主要是来了解他们的用法,先来看详细内容。

iPhone开发中了解UIALertViewUIActionSheet应用是本文要学习的内容,主要介绍UIALertViewUIActionSheet的应用,先来看详细内容。

1:构建一个简单的警告框:

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"  
  2. message:@"message"  
  3.        delegate:nil   
  4.       cancelButtonTitle:@"OK"  
  5.       otherButtonTitles:nil];  
  6. [alert show];  
  7. [alert release]; 

这里,仅仅是现实一个按钮的对话框,以模态的形式运行。

iPhone开发中了解UIALertView和UIActionSheet应用

2:当然,我们可以在一个alertView中添加多个按钮,参考代码如下所示:

 

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"   
  1.   message:@"message"  
  2.   delegate:nil   
  3.   cancelButtonTitle:@"OK"  
  4.   otherButtonTitles:@"FirstButton",@"SecondButton",  
  5.    nil];  
  6. [alert show];  
  7. [alert release]; 

iPhone开发中了解UIALertView和UIActionSheet应用

3:判断单击来那个按钮,并根据不同的按钮触发不同的事件:

alertView有一个委托(UIAlertViewDelegate),我们可以继承他,然后调用该委托来处理事件

参考代码

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"   
  2.            message:@"message" delegate:self   
  3.            cancelButtonTitle:@"OK"        
  4.            otherButtonTitles:@"FirstButton",  
  5.            @"SecondButton",nil];  
  6. [alert show];  
  7. [alert release];  
  8. //delegate   
  9. -(void)alertView:(UIAlertView *)alertView  clickedButtonAtIndex:(int)index  
  10.  
  11. {  
  12.         NSString *tt = [[NSString alloc] initWithFormat:@"%d",index];  
  13. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen"  
  14. message:tt  
  15.   delegate:self   
  16.   cancelButtonTitle:@"OK"  
  17.   otherButtonTitles:nil];  
  18. [alert show];  
  19. [alert release];  

4:手动的取消对话框

参考代码:

  1. [alert dismissWithClickedButtonIndex:0 animated:YES]; 

#p#

5:为UIAlertView添加子视图

在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。 按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。

下面的代码用来演示如何为UIAlertview对象添加子视图的方法。

  1. -(IBAction)showAlert  
  2. {  
  3. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"xingchen" message:nil delegate:nil   
  4. cancelButtonTitle:nil otherButtonTitles:nil];  
  5. [alert show];  
  6. UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]   
  7. initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];  
  8. activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);  
  9. [activeView startAnimating];  
  10. [alert addSubview:activeView];  
  11. [activeView release];  
  12. [alert release];  

显示效果如下所示:

iPhone开发中了解UIALertView和UIActionSheet应用

6:UIActionSheet

UIActionSheet也是一个模态对话框,提供类似于菜单的功能,供用户进行选择,参考代码如下所示:

iPhone开发中了解UIALertView和UIActionSheet应用 

  1. -(IBAction)showAlert  
  2. {  
  3. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheetTitle"  
  4.  
  5. delegate:nil  
  6.                cancelButtonTitle:@"Cancel"  
  7.   destructiveButtonTitle:nil  
  8.      otherButtonTitles:@"FirstButton",@"SecondButton",@"thirdButton",nil];  
  9. [sheet showInView:[self view]];  
  10. [sheet release];  
  11. }  
  12. [sheet showInView:[self view]];  

表示该UIActionsheet在那个视图里面显示。

他有一个委托是:UIActionSheetDelegate

委托的函数:

  1. @protocol UIActionSheetDelegate <NSObject> 
  2.  
  3. @optional  
  4.  
  5. // Called when a button is clicked. The view will be automatically dismissed after this call returns  
  6. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;  
  7. // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.  
  8. // If not defined in the delegate, we simulate a click in the cancel button  
  9. - (void)actionSheetCancel:(UIActionSheet *)actionSheet;  
  10. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;   
  11.  // before animation and showing view  
  12. - (void)didPresentActionSheet:(UIActionSheet *)actionSheet;   
  13.  // after animation  
  14. - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;   
  15. // before animation and hiding view  
  16. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;  
  17.   // after animation  
  18. @end 

7:Please wait ;向用户显示进度。

等待是计算机用户体验的一个部分,iphone也需要在某些情况下告知用户你所需要的数据正在加载过程中,请用户耐心等待,并需要让用户看到iphone当前确实在运行而不是已经死机。在iphone有2中有两种方式来达到该效果

  1. UIActivityIndicatorView 和UIActionSheet 

8:UIProgressBar

一个显示进度的进度条控件,他可以让用户看到程序工作的进度,指示任务完成的程度。

9:使用网络指示器:

  1. UIApplication *app = [UIApplication sharedApplication];  
  2. app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible; 

小结:iPhone开发中了解UIALertViewUIActionSheet应用的内介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-16 10:45:25

iPhone开发控件

2011-08-02 13:35:41

iOS开发 Get Post

2011-08-04 17:19:49

iPhone开发 Xcode 文档

2011-08-08 10:10:14

iPhone开发 图片 方法

2011-08-09 17:29:29

iPhone文件屏幕

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-07-19 09:58:36

2011-07-19 09:46:38

2011-07-08 14:58:16

iPhone Xcode iOS

2011-07-27 17:30:40

iPhone Locate 定位

2011-08-22 15:15:49

iPhone开发NSMutableAr排序

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-18 10:39:46

iPhone开发界面

2011-08-09 13:10:32

iPhone地图开发

2011-08-05 14:48:06

iPhone应用 异步队列

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-03 17:27:40

iPhone UIScrollVi

2011-07-28 13:59:40

iPhone App
点赞
收藏

51CTO技术栈公众号