天天看點

随波逐流之iOS AlertView(彈出框)詳解

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIButton *a = [UIButton buttonWithType:UIButtonTypeSystem];
    a.frame = CGRectMake(100, 100, 30, 30);
    a.tag = 100;
    [a setTitle:@"開始" forState:UIControlStateNormal];
    [a addTarget:self action:@selector(a1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a];
    
    
    return YES;
}

- (void)a1:(UIButton *)a
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"
        message:@"message"
        delegate:self
        cancelButtonTitle:@"Cancel"
        otherButtonTitles:@"OtherBtn",nil];
    
    //設定标題與資訊,通常在使用frame初始化AlertView時使用
    alert.title = @"AlertViewTitle";
    alert.message = @"AlertViewMessage";
    
    //這個屬性繼承自UIView,當一個視圖中有多個AlertView時,可以用這個屬性來區分
    alert.tag = 0;
    //隻讀屬性,看AlertView是否可見
    NSLog(@"%d",alert.visible);
    //通過給定标題添加按鈕
//    [alert addButtonWithTitle:@"addButton"];
    //按鈕總數
    NSLog(@"number Of Buttons :%ld",alert.numberOfButtons);
    //擷取指定索引的按鈕标題
    NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);
//    NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);
    //擷取取消按鈕的索引
    NSLog(@"cancelButtonIndex:%ld",alert.cancelButtonIndex);
    //擷取第一個其他按鈕的索引
    NSLog(@"firstOtherButtonIndex:%ld",alert.firstOtherButtonIndex);
    //顯示AlertView
    [alert show];
    [alert release];
}

#pragma marks -- UIAlertViewDelegate --
//根據被點選按鈕的索引處理點選事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickButtonAtIndex:%ld",buttonIndex);
}

//AlertView已經消失時執行的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"didDismissWithButtonIndex");
}

//ALertView即将消失時的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"willDismissWithButtonIndex");
}


//AlertView已經顯示時的事件
-(void)didPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"didPresentAlertView");
}

//AlertView即将顯示時
-(void)willPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"willPresentAlertView");
}