0

In my ViewController.m I have declared

@interface ViewController ()
{
    //...
    UIAlertView * alertView;
    //...
}

The alertview is created here:

- (void)iCloudTeavitused {
    //...
    //If the alertview happens to be previously open, it will be dismissed (I use a corresponding flag to indicate this)
    [alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex
                                    animated:YES];
    //...
    alertView = [[UIAlertView alloc] initWithTitle:AMLocalizedString(@"iCloud is available", @"iCloud is available")
                                           message:AMLocalizedString(@"This app stores", @"This app automatically stores your settings in the cloud to keep them up-to-date across all your devices")
                                          delegate:nil
                                 cancelButtonTitle:nil
                                 otherButtonTitles:AMLocalizedString(@"OK_iCloudYES", @"OK"), nil];
    [alertView show];
    //...
}

I localize words by calling

LocalizationSetLanguage(@"en");

The localization takes place in Localization.m where I it also does:

ViewController* viewController = [[ViewController  alloc]init];
[viewController iCloudTeavitused];

Thus, on some occasions, iCloudTeavitused gets called from ViewController.m also. The problem is that when it needs to dismiss the alert view (if one happens to be open) by calling

[alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex
                                animated:YES];

in iCloudTeavitused, this method actually doesn't get called (while, for example, creating another alertView DOES get called).

My guess is that dismissing the old alertView isn't fired because I'm calling this through Localization.m.

Am I right and what am I doing wrong in my code?

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
Shiim
  • 439
  • 1
  • 7
  • 14
  • 4
    Well, when you create a new ViewController, you create a *new* View Controller (unrelated to any other instances of that class). – Hot Licks Dec 25 '13 at 21:03
  • 1
    besides @HotLicks observation, i see 2 more potential issues but i am not sure. **mainly:** `...delegate:nil cancelButtonTitle:nil...` and you're trying to do something like `[alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex animated:YES];` ... will it get `cancelButtonIndex` when you have set `cancelButtonTitle:nil`? – staticVoidMan Dec 25 '13 at 21:27
  • @Hot Licks - Thanks. But what would be the best way to pass data then? Delegation? – Shiim Dec 25 '13 at 22:32
  • @staticVoidMan - no, it should work (at least, it works when called strictly from ViewController.m) – Shiim Dec 25 '13 at 22:33

2 Answers2

2

I think the issue here could be that you are dealing with multiple instances of ViewController. If you create and display an alert view using one instance of ViewController and then try to dismiss it through another one, it won't work. You should either save the instance of UIAlertView on some singleton object or your app delegate and refer that object to dismiss it before presenting the new one Or you can use browse through all the subviews in the window and dismiss a UIAlertView (if any).

for (id aSubview in [iView valueForKey:@"subviews"]) {
    if ([aSubview isKindOfClass:[UIAlertView class]]) {
        [(UIAlertView *)aSubview dismissWithClickedButtonIndex:0 animated:NO];
    }
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • @AaronBrager Thanks! I have updated my answer. I think the issue here is Shim is calling `iCloudTeavitused`from two different instances of `ViewController` objects. – Abhinav Dec 30 '13 at 01:49
  • As also Hot Licks pointed out, that's really the issue. However, I'll probably use delegation in combination with notification to solve the problem as the code for browsing through subviews doesn't seem to be very future-proof: [link](http://stackoverflow.com/questions/5399219/ios-dismiss-uialertview-beforing-showing-another) – Shiim Dec 30 '13 at 10:59
0

Make iCloudTeavitusedthe delegate of your alertView by writing:

alertView.delegate = self;

upon alertView creation.

Michal Shatz
  • 1,416
  • 2
  • 20
  • 31
  • Yes, this needs to be done for UIAlertView delegate methods to work. However, `[alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex animated:YES];` will dismiss alert view without it. – Shiim Dec 29 '13 at 22:02