一、介绍
UIAlertView这个视图控件在iOS8以后不再使用转而使用UIAlertController,UIAlertView主要用给用户显示警告信息,提示用户注意。UIAlertView跟action sheet类似,主要在外表上有所区别。
二、相关属性及方法
初始化alert view
- (instancetype nonnull)initWithTitle:(NSString * nullable)titlemessage:(NSString * nullable)message
delegate:(id nullable)delegate
cancelButtonTitle:(NSString * nullable)cancelButtonTitle
otherButtonTitles:(NSString * nullable)otherButtonTitles,
@property(nonatomic,weak) iddelegate
@property(nonatomic,copy) NSString *title
@property(nonatomic,copy)NSString *message;
增加一个按钮和其文字,并返回其增加的序列,按钮按照其增加的先后进行排序,按钮不能被自定义。
- (NSInteger)addButtonWithTitle:(NSString *)title; // returns index of button. 0 based.
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex; 返回某个序列中按钮的文字。
@property(nonatomic,readonly)NSInteger numberOfButtons; 返回按钮的个数。
@property(nonatomic)NSInteger cancelButtonIndex; // if the delegate does not implement -alertViewCancel:, we pretend this button was clicked on. default is -1
@property(nonatomic,readonly)NSInteger firstOtherButtonIndex;// -1 if no otherButtonTitles or initWithTitle:... not used
@property(nonatomic,readonly,getter=isVisible)BOOL visible;
- (void)show;
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
// alert显示给用户的类型,iOS5以后可以用。默认UIAlertViewStyleDefault
@property(nonatomic,assign)UIAlertViewStyle alertViewStyle __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex__OSX_AVAILABLE_STARTING(__MAC_NA,
三使用例子
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
alertView.title =@"用户登录";
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *titleButon= [alertView buttonTitleAtIndex:buttonIndex];
if ([titleButon isEqualToString:@"登录"]) {
NSString *userName= [alertView textFieldAtIndex:0].text;
NSString *password= [alertView textFieldAtIndex:1].text;
NSLog(@"userName=%@,password=%@",userName,password);
}
}
控制台输出:
2015-10-09 09:47:19.280 Xcode_learn[1317:50099] userName=tyyt,password=hhhh