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

ios UIAlertView使用全解

2018年02月14日 ⁄ 综合 ⁄ 共 5673字 ⁄ 字号 评论关闭

UIAlertView使用全解

1.

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"Default AlertView"message:@"Defalut" delegate:selfcancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

 标准的双按钮,cancel那个buttonIndex
0 ok button
buttonIndex1


 2.

 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Default Alert View"message:@"Defalut"delegate:self cancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",@“ThirdButton”, nil]; 

 和程序里的顺序一样,cancel  
ok   thirdButton
buttonIndex
分别为0 1 2


3.

 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Default Alert View"message:@"Defalut"delegate:self cancelButtonTitle:@"Cancel"otherButtonTitles:@"OK",@“ThirdButton”, nil];

 同理,cancel  
ok   thirdButton FourthButton
buttonIndex
分别为0 1 2 3

[alertView show];

 

4. 手动的取消对话框

 

 [alertdismissWithClickedButtonIndex:0animated:YES];

 

5:为UIAlertView添加子视图


 在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。

 下面的代码用来演示如何为UIAlertview对象添加子视图的方法。

 UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"请等待"

 

                                                 message:nil

 

                                                 delegate:nil  

 

                                                 cancelButtonTitle:nil

 

                                                 otherButtonTitles:nil]; 

 

 [alert show];

 UIActivityIndicatorView*activeView =[[UIActivityIndicatorViewalloc]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];

 

 }

  显示将消息框固有的buttonlabel移位,不然添加的text
field
会将其遮盖住。 然后添加需要的部件到相应的位置即可。

 

 对于UIActionSheet其实也是一样的,

 -(void)willPresentActionSheet:(UIActionSheet *)actionSheet

 中做同样的处理一样可以得到自己想要的界面。

  

UIAlertView Delegate

- voidalertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

alertView--->这个不用多说了吧

buttonIndex---->0开始

可以通过if (buttonIndex ==1) { }
这样的来控制点击了某个按钮需要做什么操作

 

- voidalertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

这个方法在动画结束和视图隐藏之后调用

 

- voidalertView:(UIAlertView*)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

这个方法在动画开始和视图隐藏之前调用

 

- voidalertViewCancel:(UIAlertView*)alertView

 在视图将要被取消之前

 例如,用户点击了home

 三个函数的调用顺序依次是:

 alertViewCancel----willDismissWithButtonIndex---didDismissWithButtonIndex

 

- BOOLalertViewShouldEnableFirstOtherButton(UIAlertView*)alertView

ios 5+

设置yes / no 
将会设置alertView 的第一个otherButtonenable属性

 

- void)didPresentAlertView:(UIAlertView *)alertView

 在视图提交给用户以后调用

 

- (void)willPresentAlertView:(UIAlertView *)alertView

 在视图提交给用户以前调用

 

这六个delegate
方法调用的顺序依次是

 alertViewShouldEnableFirstOtherButton---->willPresentAlertView--->didPresentAlertView

 ---->clickedButtonAtIndex---->(如果会触发视图取消,则会调用alertViewCancel)willDismissWithButtonIndex---->didDismissWithButtonIndex

 

ios4.0以后 alertView不会自动随着程序转向后台而移除

 alertView属性

 1.alertViewStyle:

 UIAlertViewStyleDefault
只弹信息和按钮

 UIAlertViewStyleSecureTextInput
有一个textfield加密框

 UIAlertViewStylePlainTextInput
有一个不加密的textfield

 UIAlertViewStyleLoginAndPasswordInput
有两个textfieldLoginpassword

 

 只要有textfield就可以用textfieldAtIndex来捕获并进行相应的操作例如换键盘类型

 

 2.cancelButtonIndex

 开始是0,如果没有设置cancel button
则是-1

 

 3.delegate

 如果没有设置则是nil

 

 4.firstOtherButtonIndex

 0开始,如果没设置则是-1,而且没被设置则会被忽略

 

 5.message

消息

 

 6.numberOfButtons

只读 
alertView
中的按钮数量

 

 7.title

标题

 

 8.visible

只读 
如果是yes 表示被显示

 

实例方法

- (NSInteger)addButtonWithTitle:(NSString*)title

 返回值是增加的Buttonindex

 

- (NSString*)buttonTitleAtIndex:(NSInteger)buttonIndex

 输入buttonIndex
返回button的标题

 

- void)dismissWithClickedButtonIndex:(NSInteger)buttonIndexanimated:(BOOL)animated

 程序自动完成点击buttonIndexbutton
dismiss 整个alertView的操作

 

- (id)initWithTitle:(NSString *)titlemessage:(NSString)message delegate:(id)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitles, ...

 这个就不用多说了

 

- (void)show

 要显示必须要调用这个alertview才会显示

 

- (UITextField *)textfieldAtIndex:(NSInteger)textfieldIndex

 返回值是textfield

 UIAlertViewStyleDefault
没有

 UIAlertViewStyleSecureInput textfieldIndex
只有一个为0

 UIAlertViewStylePlainInput textfieldIndex
只有一个为0

 UIAlertViewStyleLoginAndPasswordInputtextfieldIndex有两个 0 1

抱歉!评论已关闭.