IOS弹出对话框让用户等待

今天看《iPhone开发秘籍》,有段代码非常优雅的完成了这个功能,如下:

[html] view plain copy
  1. -(void)performDismiss
  2. {
  3. [baseAlertdismissWithClickedButtonIndex:0animated:NO];
  4. }
  5. -(void)action:(UIBarButtonItem*)item
  6. {
  7. baseAlert=[[[UIAlertViewalloc]initWithTitle:@"PleaseWait"message:nildelegate:selfcancelButtonTitle:nilotherButtonTitles:nil]autorelease];
  8. [baseAlertshow];
  9. //Createandaddtheactivityindicator
  10. UIActivityIndicatorView*aiv=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  11. aiv.center=CGPointMake(baseAlert.bounds.size.width/2.0f,baseAlert.bounds.size.height-40.0f);
  12. [aivstartAnimating];
  13. [baseAlertaddSubview:aiv];
  14. [aivrelease];
  15. //Autodismissafter3seconds
  16. [selfperformSelector:@selector(performDismiss)withObject:nilafterDelay:3.0f];
  17. }

首先,显示一个无按钮的alertView,然后添加一个UIActivityIndicatorView作为alertView的子视图

最后,通过alertView的dismissWithClickedButtonIndex:animated:取消显示alertView。

代码和显示效果都非常优雅

其中,UIActivityIndicatorView就是代替了我之前代码中gif图片的作用。


转自http://blog.csdn.net/xianpengliu/article/details/6591592

你可能感兴趣的:(ios)