介绍UIALertView和UIActionSheet的用法。
2012-06-15 11:46
411 查看
介绍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];
这里,仅仅是现实一个按钮的对话框,以模态的形式运行。
![](http://hiphotos.baidu.com/287971051/pic/item/65ef75170924ab186d53de8635fae6cd7a890b40.jpg)
2:当然,我们可以在一个alertView中添加多个按钮,参考代码如下所示:
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen"
message:@"message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:@"FirstButton",@"SecondButton",
nil];
[alert show];
[alert release];
![](http://hiphotos.baidu.com/287971051/pic/item/e79fac25ab18972b2f7f3f7be6cd7b899f510a40.jpg)
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];
}
显示效果如下所示:
![](http://hiphotos.baidu.com/287971051/pic/item/84904ec5b74543a99b7cc57d1e178a82b80114a1.jpg)
6:UIActionSheet
UIActionSheet也是一个模态对话框,提供类似于菜单的功能,供用户进行选择,参考代码如下所示:
![](http://hiphotos.baidu.com/287971051/pic/item/dc4d124443a98226d5a614968a82b9014b90eba2.jpg)
![](http://hiphotos.baidu.com/287971051/pic/item/3ead0e19972bd407c982ec4c7b899e510eb30940.jpg)
-(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;
write by Xingchen’s sky
1:构建一个简单的警告框:
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen"
message:@"message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
这里,仅仅是现实一个按钮的对话框,以模态的形式运行。
![](http://hiphotos.baidu.com/287971051/pic/item/65ef75170924ab186d53de8635fae6cd7a890b40.jpg)
2:当然,我们可以在一个alertView中添加多个按钮,参考代码如下所示:
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"xingchen"
message:@"message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:@"FirstButton",@"SecondButton",
nil];
[alert show];
[alert release];
![](http://hiphotos.baidu.com/287971051/pic/item/e79fac25ab18972b2f7f3f7be6cd7b899f510a40.jpg)
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];
}
显示效果如下所示:
![](http://hiphotos.baidu.com/287971051/pic/item/84904ec5b74543a99b7cc57d1e178a82b80114a1.jpg)
6:UIActionSheet
UIActionSheet也是一个模态对话框,提供类似于菜单的功能,供用户进行选择,参考代码如下所示:
![](http://hiphotos.baidu.com/287971051/pic/item/dc4d124443a98226d5a614968a82b9014b90eba2.jpg)
![](http://hiphotos.baidu.com/287971051/pic/item/3ead0e19972bd407c982ec4c7b899e510eb30940.jpg)
-(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;
相关文章推荐
- 介绍UIALertView和UIActionSheet的用法。
- iphone开发 UIActionSheet(操作表) 和UIAlertView(警告)的用法
- iOS开发 对话框UIActionSheet和UIAlertView用法
- iOS9中UIAlertView和UIActionSheet的用法
- iOS UIAlertView和UIActionSheet的用法
- UIAlertView和UIActionSheet
- MMPopupView(自定义UIAlertView、UIActionSheet、UIDatePicker)
- iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?
- UIActionSheet与UIAlertView
- 2013-8-12练习[制作一个具有UIAlertView和UIActionSheet的登陆界面]
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet)
- 如何自定义UIActionSheet(UIAlertView)中的内容
- UIAlertView、 UIActionSheet
- 如何自定义UIActionSheet(UIAlertView)中的内容
- 2013-8-12练习[制作一个具有UIAlertView和UIActionSheet的登陆界面]
- UIActionSheet,UIAlertView技术分享
- UIAlertView、UIActionSheet、UIAlertController使用
- UIAlertView、UIActionSheet兼容iOS8
- UIActionSheet/ UIAlaterView/UIAlertController
- 【iPhone代码片段之一】UIActionSheet&UIAlertView的应用