天天看点

关于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);

}

如有错误,请大鸟指点