简体   繁体   中英

How to disable a UIAlertView button with ios 7?

I have a simple view with a button linked with "showAlert" method. When I click on this button, it displays a UIAlertView.

Before, with ios 6, I was using the following code to disable a UIAlertView button :

- (IBAction)showAlert:(id)sender
{
    myAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Retour" otherButtonTitles:@"Button1", @"Button2", @"Button3", @"Button4", nil];
    [myAlert show];

    for(UIView *aView in myAlert.subviews)
    {
        if ([[[aView class] description] isEqualToString:@"UIAlertButton"])
        {
            UIButton *aButton = (UIButton *)aView;
            if ([aButton.titleLabel.text isEqualToString:@"Button2"])
                aButton.enabled = NO;
        }
    }
}

Now, with ios 7, it does not work... Why ?

Since iOS7 it's not possible to add or manipulate the subviews or a UIAlertView , you need to create your own, sorry.

Subclass UIView to create your own UIAlertView or use a 3rd party library.

Adding a subview to UIAlertView is not possible from iOS 7.

The only way is to go for a custom UIView subclass which can act as UIAlertView .

Github and this answer may get you a solution.

You can disable 1st other Button of UIAlertView using delegate method

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return NO;
}

works in ios 7 also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM