0

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 !!!

Prasanna
  • 945
  • 10
  • 24
  • 1
    This question needs code. Before you paste in too much, I suggest making a tiny tab-based app with 2-3 tabs whose view controllers do nothing but open a UIAlertView when a button is pressed. Make the crash happen and post that code + the crash log. Bonus: making the tiny app might cause you to figure out the answer. – danh May 07 '14 at 15:33
  • @danh Thanks! for responding my post,i have updated my post with the CRASH log,and the problem is not by any of the existing code,its the issue of the alert "self" object deallocation.When the i am changing the view on coming the app foreground and the alert object with the above scenario is deallocated. – Prasanna May 07 '14 at 15:41
  • so as far i understand basically your problem is with the viewcontroller which is presenting the uialertview, and it gets deallocated,".how to keep it alive/undeallocated?? – maddy May 07 '14 at 15:43
  • @Alok that is the Question... – Prasanna May 07 '14 at 15:45
  • i would suggest you to have a strong property to the view controller which is showing alert view, because ARC dealloc the weakly pointed objects. – maddy May 07 '14 at 15:45
  • i have around 250 alerts in my app...@Alok – Prasanna May 07 '14 at 15:46
  • But the zombie vc is one of the tabs? That can't be. Those are retained by the UITab vc. Aren't they? Code is so much clearer than text. Please add code. – danh May 07 '14 at 15:47
  • @danh is right please put some code how are you architecting UITAbbar – maddy May 07 '14 at 15:49

2 Answers2

1

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];
}
}
nmh
  • 2,497
  • 1
  • 15
  • 27
0

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;
}
Prasanna
  • 945
  • 10
  • 24