2013年8月7日星期三

IOS development: UIAlertView using

 

UIAlertView what is not introduced

 

1. Basic usage

 
  
   
1 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"Test"    //标题 
2 message:@"this is a alert view " //显示内容
3 delegate:nil //委托,可以点击事件进行处理
4 cancelButtonTitle:@"取消"
5 otherButtonTitles:@"确定"
6 //,@"其他", //添加其他按钮
7                                  nil];
8 [view show];

  
 
 

renderings:

 

 

2. multiple buttons

 

cancel the above code @ "other" notes, run the effect is as follows

 

 

be so, add multiple

 

3. some system style parameters

 

UIAlertViewStyle This enumeration provides several styles

 
  
1 typedef NS_ENUM(NSInteger, UIAlertViewStyle) { 
2 UIAlertViewStyleDefault = 0,           //缺省样式
3 UIAlertViewStyleSecureTextInput,         //密文输入框
4 UIAlertViewStylePlainTextInput,         //明文输入框
5 UIAlertViewStyleLoginAndPasswordInput      //登录用输入框,有明文用户名,和密文密码输入二个输入框
6 };
 
 

 

using the code below:

 
  
1     UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"    //标题 
2 message:@"this is a alert view " //显示内容
3 delegate:nil //委托,可以点击事件进行处理
4 cancelButtonTitle:@"取消"
5 otherButtonTitles:@"确定",
6 //,@"其他", //添加其他按钮
7 nil];
8 [view setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; //控制样式
 
 

renderings:

 

 

this parameter is: UIAlertViewStyleLoginAndPasswordInput renderings, other self-view

 

But these types, I personally feel ugly and unacceptable, then customize a pop-up box, used to accept input

 

 

 

is not difficult to achieve, a friend in need can contact me

 

4. determine which button the user point

 

UIAlertView delegate UIAlertViewDelegate, implement the delegate to achieve click event, as follows:

 

. h file

 
  
1 @interface ViewController : UIViewController<UIAlertViewDelegate> { 
2
3 }
 
 

 

in. m implement delegate methods

 
  
1 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
2 {
3 NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
4 NSLog(@"%@",msg);
5 }
 
 

parameters in this method buttonIndex, represents the index of the button, the figure of the three buttons "Cancel", "OK", "Other" corresponds to the index are "0", "1" , "2".

 

manner with Delegate click, will bring a more troublesome problem, such as a page, there are several UIAlertView when handling click it, it will increase the complexity of processing logic, have to make some judgments < / p>  

this situation there is a solution, is to use Block, Block added a callback instead Delegate, target and selector . (next Expand write this content )

 

5. adding subviews

 

This use was also more and more, posted a few use cases

 

add UIActivityIndicatorView

 

 

 

implementation code:

 
  
 1     UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待" 
2 message:nil
3 delegate:nil
4 cancelButtonTitle:nil
5 otherButtonTitles:nil,
6 nil];
7
8 UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
9 activeView.center = CGPointMake(view.bounds.size.width/2.0f, view.bounds.size.height-40.0f);
10 [activeView startAnimating];
11 [view addSubview:activeView];
12
13 [view show];
 
 

 

add UITableView

 

 

this list a few lines of code do not know, say, the next idea, right, UIAlertView reason why there is so much space display UITableView, with a relatively tricky one way

 
  
1 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请选择" 
2 message:@"\n\n\n\n\n\n\n\n\n\n"
3 delegate:nil
4 cancelButtonTitle:nil
5 otherButtonTitles:nil,
6 nil];
7 //其中用了10个换行符来撑大UIAlertView的
 
 

then come back to add UITableView, can be self-fulfilling, if there is a need, please leave a message

 

Basically, this is some of the more common and useful things, then there is one more important thing is customize and beautify UIAlertView , I believe that many people care about this, customize and beautify the contents on the next one to go into detail, analyze several individuals feel good Demo source

 

没有评论:

发表评论