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?

4
  • 4
    Well, when you create a new ViewController, you create a new View Controller (unrelated to any other instances of that class). Commented Dec 25, 2013 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? Commented Dec 25, 2013 at 21:27
  • @Hot Licks - Thanks. But what would be the best way to pass data then? Delegation? Commented Dec 25, 2013 at 22:32
  • @staticVoidMan - no, it should work (at least, it works when called strictly from ViewController.m) Commented Dec 25, 2013 at 22:33

2 Answers 2

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];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@AaronBrager Thanks! I have updated my answer. I think the issue here is Shim is calling iCloudTeavitusedfrom two different instances of ViewController objects.
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
0

Make iCloudTeavitusedthe delegate of your alertView by writing:

alertView.delegate = self;

upon alertView creation.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.