天天看點

關于ios 中 UIAlertView

在iOS7 中靠列舉alertview中的對像修改alertview已經不行了

//在iOS6中建立AlertView,

UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"這裡是提示标題" message:@"這裡是提示内容" delegate:self cancelButtonTitle:@"最下邊的按鈕" otherButtonTitles:@"按鈕1",@"按鈕2",@"按鈕3",nil];

            //alertView1.delegate = self;//代理可以在建立以後再設定,也可以在建立時直接設定,但是必須在show之前(不然一個代理方法不執行)。

[alertView1 show];

[alertView1 release];

//以下6個方法為alertview的代理方法

- (void)alertViewCancel:(UIAlertView *)alertView

{

//在點選home鍵的時候執行,模拟器裡測試不出來

    NSLog(@"取消是運作  ---%d",alertView.tag);

}

- (void)willPresentAlertView:(UIAlertView *)alertView

{

//如果在show之後,設定代理則不會執行了。

    NSLog(@"将要顯示的時候運作 ---%d",alertView.tag);

    //可以對alertview做簡單的自定義,如下

    CGRect frame = alertView.frame;

    if( alertView == alertView1 )//如果alertView就是聲明的alertView1

    {

        frame.origin.y -= 80;

        frame.size.height += 80;

        alertView.frame = frame;

        for( UIView * view in alertView.subviews )//列舉alertView中所有的對象

        { 

            if( ![view isKindOfClass:[UILabel class]] )//如果對象不是UILabel

            {

                //isKindOfClass判斷不是UILable的,則另行處理

                if (view.tag==1){

//處理第一個按鈕CancelButton進行設定

                    CGRect btnFrame1 =CGRectMake();

                    view.frame = btnFrame1; 

                } else if (view.tag==2){

                    //處理第二個按鈕otherButton

                    CGRect btnFrame2 =CGRectMake();

                    view.frame = btnFrame2;

                } else if (view.tag==3){

                    //處理第三個按鈕otherButton

                    CGRect btnFrame2 =CGRectMake();

                    view.frame = btnFrame2;

                    view.backgroundColor = [UIColor redColor];

                }

            }

        }

    }

    //在alertVIew上添加按鈕

    //UIButton *but = [[UIButton alloc] initWithFrame:CGRectMake((frame.size.width-100)/2, frame.size.height/2, 100, 40)];

//    but.backgroundColor = [UIColor greenColor];

//    [but addTarget:self action:@selector(butclick) forControlEvents:UIControlEventTouchUpInside];

//    [alertView addSubview:but];

    text = [[UITextField alloc] initWithFrame:CGRectMake(40, 100, 200, 40)];

    text.borderStyle = UITextBorderStyleRoundedRect;

    text.keyboardType = UIKeyboardTypeNumberPad;//改變鍵盤的類型

    [alertView addSubview:text];

}

-(void)butclick

{

    NSLog(@"%@",text.text);

    NSLog(@"234");

    [alertView1 dismissWithClickedButtonIndex:0 animated:YES];

}

// before animation and showing view

- (void)didPresentAlertView:(UIAlertView *)alertView

{// after animation

    NSLog(@"已經顯示後執行該方法  -----%@",alertView.title);

}

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

{

    NSLog(@"被點選的時候執行該方法----%d",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)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

{// before animation and hiding view

    NSLog(@"将要消失執行該方法  ----%@",alertView.message);

}

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

{// after animation

    NSLog(@"已經消失執行該方法   -----%d    === %d",alertView.tag,buttonIndex);

}

如有錯誤,請大鳥指點