现在的位置: 首页 > 综合 > 正文

介绍UIALertView和UIActionSheet的用法。

2013年05月23日 ⁄ 综合 ⁄ 共 3587字 ⁄ 字号 评论关闭

介绍UIALertView和UIActionSheet的用法。
write by Xingchen’s sky
1:构建一个简单的警告框:
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen"
message:@"message"
      delegate:nil 
    cancelButtonTitle:@"OK"
    otherButtonTitles:nil];
[alert show];
[alert release];
这里,仅仅是现实一个按钮的对话框,以模态的形式运行。

2:当然,我们可以在一个alertView中添加多个按钮,参考代码如下所示:
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen" 
 message:@"message"
 delegate:nil 
  cancelButtonTitle:@"OK"
 otherButtonTitles:@"FirstButton",@"SecondButton",
  nil];
[alert show];
[alert release];

3:判断单击来那个按钮,并根据不同的按钮触发不同的事件:
alertView有一个委托(UIAlertViewDelegate),我们可以继承他,然后调用该委托来处理事件
参考代码
 
 
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"xingchen"
         message:@"message" delegate:self 
         cancelButtonTitle:@"OK"     
         otherButtonTitles:@"FirstButton",
        @"SecondButton",nil];
[alert show];
[alert release];
//delegate 
 
-(void)alertView:(UIAlertView*)alertView  clickedButtonAtIndex:(int)index
{
       NSString *tt = [[NSStringalloc] initWithFormat:@"%d",index];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen"
message:tt
  delegate:self 
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil];
[alert show];
[alert release];
}
4:手动的取消对话框
参考代码:
[alertdismissWithClickedButtonIndex:0 animated:YES];
5:为UIAlertView添加子视图
在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
下面的代码用来演示如何为UIAlertview对象添加子视图的方法。
-(IBAction)showAlert
{
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"xingchen"message:nildelegate:nil 
 cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
 
UIActivityIndicatorView *activeView= [[UIActivityIndicatorView alloc] 
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activeView.center =CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);
 
[activeView startAnimating];
[alert addSubview:activeView];
[activeView release];
 
[alert release];
 
}
显示效果如下所示:

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


-(IBAction)showAlert
{
UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:@"ActionSheetTitle"
delegate:nil
            cancelButtonTitle:@"Cancel"
 destructiveButtonTitle:nil
    otherButtonTitles:@"FirstButton",@"SecondButton",@"thirdButton",nil];
[sheet showInView:[self view]];
[sheet release];
   
}
[sheet showInView:[self view]];表示该UIActionsheet在那个视图里面显示。

他有一个委托是:UIActionSheetDelegate
委托的函数:
@protocol UIActionSheetDelegate<NSObject>
@optional
 
//Called when a button is clicked. The view will be automaticallydismissed after this call returns
-(void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex;
 
//Called when we cancel a view (eg. the user clicks the Home button).This is not called when the user clicks the cancel button.
// Ifnot defined in the delegate, we simulate a click in the cancelbutton
-(void)actionSheetCancel:(UIActionSheet *)actionSheet;
 
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
-(void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation
 
-(void)actionSheet:(UIActionSheet *)actionSheetwillDismissWithButtonIndex:(NSInteger)buttonIndex; // beforeanimation and hiding view
-(void)actionSheet:(UIActionSheet *)actionSheetdidDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation
 
@end
 

等待是计算机用户体验的一个部分,iphone也需要在某些情况下告知用户你所需要的数据正在加载过程中,请用户耐心等待,并需要让用户看到iphone当前确实在运行而不是已经死机。在iphone有2中有两种方式来达到该效果
UIActivityIndicatorView 和UIActionSheet
 
 
8:UIProgressBar
一个显示进度的进度条控件,他可以让用户看到程序工作的进度,指示任务完成的程度。
9:使用网络指示器:
 
UIApplication *app =[UIApplication sharedApplication];
 
app.networkActivityIndicatorVisible =!app.networkActivityIndicatorVisible;

抱歉!评论已关闭.