1. 最简单的用法
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"这是一个简单的警告框!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
2. 为UIAlertView添加多个按钮
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
3. 如何判断用户点击的按钮
UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件
头文件:
@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;
@end
源文件:
-(IBAction) buttonPressed
{
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3
4. 手动的取消对话框
[alertdismissWithClickedButtonIndex:0 animated:YES];
5:为UIAlertView添加子视图
在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
下面的代码用来演示如何为UIAlertview对象添加子视图的方法。
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"请等待"
message:nil
delegate: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. UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}
这段代码很简单, 就是在消息框即将弹出时,遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentLeft即可。
添加其他部件也如出一辙, 如下代码添加两个UITextField:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
CGRect frame = alertView.frame;
frame.origin.y -= 120;
frame.size.height += 80;
alertView.frame = frame;
for( UIView * viewin alertView.subviews )
{
if( ![viewisKindOfClass:[UILabelclass]] )
{
CGRect btnFrame = view.frame;
btnFrame.origin.y += 70;
view.frame = btnFrame;
}
}
UITextField* accoutName = [[UITextFieldalloc] init];
UITextField* accoutPassword = [[UITextFieldalloc] init];
accoutName.frame = CGRectMake( 10, frame.origin.y + 40,frame.size.width - 20, 30 );
accoutPassword.frame = CGRectMake( 10, frame.origin.y + 80,frame.size.width -20, 30 );
accoutName.placeholder = @"请输入账号";
accoutPassword.placeholder = @"请输入密码";
accoutPassword.secureTextEntry = YES;
[alertView addSubview:accoutPassword];
[alertView addSubview:accoutName];
[accoutName release];
[accoutPassword release];
}
显示将消息框固有的button和label移位, 不然添加的text field会将其遮盖住。 然后添加需要的部件到相应的位置即可。
对于UIActionSheet其实也是一样的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同样的处理一样可以得到自己想要的界面。
相关推荐
UIAlertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
iPhone入门 - UIAlertView用法
详细讲解UIAlertView和UIActionSheet的使用方法,以及开发中遇到的一些问题
基于关联和block写的UIAlertView回调.一些关联的基本用法。
使用方法和示例 UIAlertCategory * a = [[UIAlertCategory alloc] initWithTitle:@"警告" WithMessage:@"你有条警告"]; [a addButton:ALERT_BUTTON_OK WithTitle:@"好的" WithAction:^(void *action) { ...
1. 最简单的用法 初始化方法: 代码如下: – (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /**/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle ...
带有块的UIAlertView 不再需要使用委托方法。 用户可以使用Block创建UIAlertView。 用户传递参数时,必须以数组的方式传递按钮。 按钮索引将带有块。 [UIAlertView showAlertWithTitle: @" title " message: @" this...
自从我第一次在应用中使用UIActionSheet或者UIAlertView时,我不喜欢它的实现方式。 如果在同一类中有两种警报,那么就很痛苦了,例如通过调用委托方法来完成所有。 我也不喜欢在按钮的事件中应该执
但是在使用的时候并不是很方面,有一些不足,于是我参照Android系统AlertDialog,使用参考链接中的布局文件和style文件,用自己的方法自定义了一下这个仿IOS上面UIAlertView的效果,这样的话让我们可以想使用系统...
所以就仿照系统的 UIAlertView 原始功能,支持使用 NSAttributedString ,这样就可以自定义文本格式了,效果如下图; ###支持的功能: 支持 叠加 的弹出 多个 Alert框; 适配 iOS6、7、8, 弹框支持随着屏幕的 方向...
作者yimouleng,源码UIAlertCategory,简单的 UIAlert 封装 如果 (iOS <= 7)使用 UIAlertView 如果 (iOS >= 8) UIAlertViewController. 使用方法和示例。
主要介绍了iOS中UIAlertView3秒后消失的两种实现方法,实现方法涉及到NSTimer和PerformSelector:withObject:afterDelay:方法的结合使用,需要的朋友可以参考下
自定义的UIAlertView,使用方法和系统的UIAlertView一样。
整合了UIAlertView和UIActionSheet这两种系统风格的弹框,创建弹框再也不用兼容iOS8以前和iOS8以后了。代码也不会因为delegate而分离,直接接受Block回调。如果您觉得我的代码帮到了您,我会非常欣慰。同时如果您想...
简单的 Popover 替换原生 UIAlertView。 要求 Xcode 6 或更高版本 苹果LLVM编译器 iOS 6.0 或更高版本 弧 演示 在 Xcode 中构建并运行TYSimplePopoverDemo项目。 安装 将TYSimplePopoverViewController.h和...
用法 要运行示例项目,请克隆存储库,然后首先从Example目录运行pod install 。 不显示任何动作警报 [ARNAlert showNoActionAlertWithTitle: @" no action title " message: @" no action message " buttonTitle: @...
枪械警报GUNAlert 是 UIAlertView 和 UIAlertController 的简单包装器。 适用于需要与 iOS 版本 < 8 保持向后兼容性的应用程序。它被设计为适用于大多数常见用例的简单解决方案。用法例子要运行示例项目, pod ...
Apple 的 UIAlertView 的基于块的替代品。 用法: 只需将 MSSAlertViewController.h/.m 添加到您的项目中,您就可以开始使用了! 这是一个基本的“仅确定”警报: MSSAlertViewController *alert = ...
用法 要运行示例项目, pod install克隆 repo,然后从 Example 目录运行pod install 。 安装 TAlertView 可通过。 要安装它,只需将以下行添加到您的 Podfile 中: pod "TAlertView" 如何使用它 简单的 [[...
使用方法: MyURLClass *post = [MyURLClass urlGo]; post.DataBlock = ^(NSData *data){ NSError *error; if (data == Nil) { UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络...