Access all UIAlertView object as a global object to control it Universally(to resolve CRASH in alert after deallocating the view contoller)

StackOverflow https://stackoverflow.com/questions/23521832

Question

My requirement is to make my app's third tab as the home screen,and also whenever the user moves the app in background and while again moving the app to foreground it should be the home screen as our third tab instead the app is in any of the places in the app. This issue is handled(making third tab as home screen instead the app is in any of the places/scenarios in the app).,But i have an issue now which is generated by this resolution.

Problem :- If any alert is displaying in any view and we are making the app background-foreground ,the app comes to the third tab from that screen,and the alert is still there and if we click on the alert button ,then as per the rules of IOS the "self" object of the screen of alert is deallocated(because now we are in the home screen and the alert is here) and app CRASHES !

Tried some resolutions :-

1.In a screen ,I made an global object of UIAlertView and using below line of code in applicationDidBecomeActive method of the Screen...

[_alertToRemoveContact dismissWithClickedButtonIndex:1 animated:NO];

This is working code for this view,but my problem with resolution is that i need to create a global object of alert view in all places of the app which is a very much time consuming task because i am using around 250 alerts in the project.

2.I am killing the app whenever it moves to background ,In this resolution the problem is that my app will not work its downloading functionality in background cause the app is killed.

Need help for the resolution of this issue if any one need more explanations,please leave comments.

My Crash Log.... * -[ContactShowViewController respondsToSelector:]: message sent to deallocated instance 0x1138c4e0*

*Where ContactShowViewController will differ a/c to the screen Thanks in advance !!!

Was it helpful?

Solution 2

I have resolved this issue with the help of below solution :-

1.I created an app delegate instance of UIAlertView.

2.I implemented an alert view delegate method "will present alert view...",this method gives me all the alert view objects as a parameter where i have assigned it to my app delegate object of alert view.

3.On application life cycle method "applicationDidEnterBackground" i am using below code ...,which resigns my alert dailog on coming from background to foreground...

if ([AppDelegate shared].alertObserver) {

  //Dismissing alert which was shown before moving to background

    [[AppDelegate shared].alertObserver dismissWithClickedButtonIndex:0 animated:NO];
    [AppDelegate shared].alertObserver=nil;
}

OTHER TIPS

Let's try singleton:

__strong static AlertUtil* _sharedInstance = nil;
@implementation AlertUtil
{
UIAlertView *alertView;
}
+ (AlertUtil *)sharedInstance
{
@synchronized(self)
{
    if (nil == _sharedInstance)
    {
        _sharedInstance = [[AlertUtil alloc] init];
    }
}
return _sharedInstance;
}
- (void)showConfirmAlertWithMessage:(NSString *)msg cancelBtnTitle:(NSString *)btn1 okbtnTitle:(NSString *)btn2 delegate:(id)delegate tag:(int)tag
{
alertView = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:delegate cancelButtonTitle:btn1 otherButtonTitles:btn2, nil];
alertView.tag = tag;
[alertView show];
}
- (void)cancel
{
if (alertView){
    [alertView dismissWithClickedButtonIndex:0 animated:NO];
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top