iOS基础 -- UIAlertView

UIAlertView主要就是弹出alert窗口,常见使用方式如下:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"you're been delived an alert" delegate:nil cancelButtonTitle:@"Cancle" otherButtonTitles:@"ok",@"not sure",@"yes", nil];
    [alertView show];

显示title、msg以及多个选择按钮。UIAlertView的点击状态的捕捉,主要是通过 UIAlertViewDelegate 协议来实现。 UIAlertViewDelegate 协议的定义如下:

@protocol UIAlertViewDelegate 
@optional

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(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.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(UIAlertView *)alertView;

- (void)willPresentAlertView:(UIAlertView *)alertView;  // before animation and showing view
- (void)didPresentAlertView:(UIAlertView *)alertView;  // after animation

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation

// Called after edits in any of the default fields added by the style
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;

@end

常用的使用方式如下:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:[self yesButtonTitle]] == YES){
        NSLog(@"user click the yes button");
    }
    if([buttonTitle isEqualToString:[self noButtonTitle]] == YES){
        NSLog(@"user click the no button");
    }
//    NSLog(@"%@",[[alertView textFieldAtIndex:0] text]);
//    NSLog(@"%@",[[alertView textFieldAtIndex:1] text]);
    
}

UIAlertView有 AlertViewStyle 这个属性,用来设置不同的形式。主要有以下形式:

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
};

UIAlertViewStyleDefault 是默认的,如上例所示的样子。

UIAlertViewStyleSecureTextInput 是隐藏输入的字符。

UIAlertViewStylePlainTextInput 是显示输入框

UIAlertViewStyleLoginAndPasswordInput 是显示登录、密码输入框


你可能感兴趣的:(iOS,iOS,iOS基础,UIAlertView)