介绍UIALertView和UIActionSheet的用法。

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
 
7:Please wait ;向用户显示进度。
等待是计算机用户体验的一个部分,iphone也需要在某些情况下告知用户你所需要的数据正在加载过程中,请用户耐心等待,并需要让用户看到iphone当前确实在运行而不是已经死机。在iphone有2中有两种方式来达到该效果
UIActivityIndicatorView 和 UIActionSheet
 
8:UIProgressBar
一个显示进度的进度条控件,他可以让用户看到程序工作的进度,指示任务完成的程度。
9:使用网络指示器:
UIApplication *app =[UIApplication sharedApplication]; 
app.networkActivityIndicatorVisible =!app.networkActivityIndicatorVisible;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义UIAlertView的方法已经在iOS13之后被废弃,推荐使用UIAlertController来代替。下面介绍如何自定义UIAlertController的弹出位置以及宽度。 1. 自定义弹出位置 可以使用UIAlertController的popoverPresentationController属性来设置弹出位置。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 创建弹出位置的UIView对象 UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; // 设置弹出位置 alertController.popoverPresentationController.sourceView = popoverView; alertController.popoverPresentationController.sourceRect = popoverView.bounds; // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 2. 自定义宽度 可以通过设置UIAlertController的preferredContentSize属性来改变其宽度。具体步骤如下: ``` // 创建UIAlertController对象 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; // 设置宽度 alertController.preferredContentSize = CGSizeMake(280, 200); // 弹出UIAlertController [self presentViewController:alertController animated:YES completion:nil]; ``` 需要注意的是,此方法只适用于UIAlertControllerStyleAlert风格的UIAlertController。如果使用UIAlertControllerStyleActionSheet风格的UIAlertController,设置preferredContentSize属性将不会生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值