2

I'm trying to create an custom alert-like view that is to display while data is loading, I've also got a cancel button on it in-case the loading takes too long. I'm creating it in a ViewController and adding it as a subview to the window (not View Controller as I want the Loading alert view to handle itself as much as possible). The view displays fine but when I click the button I get a 'message sent to deallocated instance' I've used the Zombie tool and it appears that everything that retains it is just releasing it right away (I'm using ARC).

I have noticed that if I use a global variable and assign my loading alert view to that it works, but that is not the desired effect. I want to be able to show it just like an Alertview. Here is my 'show' function, what can I do so that it retains itself(?).

    - (void) show{

    //this is so the user can't accidentally hit a button in the parent view while the
    // alert is active
    parentView.view.userInteractionEnabled = NO;

    //I get the window
    id appDelegate = [[UIApplication sharedApplication] delegate];
    UIWindow *window = [appDelegate window];

    //I set up the view and create/assign the correct placement of it as well as 
    //make it invisible so I can fade it in later.
    self.view.alpha = 0.0;
    self.view.frame = [self createFrameFor:self.view withOffset:0];

    //since the view is smaller that the window I create an image that is the size 
    //of the window but semi-transparent
    UIImage *bg = [self setupBackground];
    backgroundView = [[UIImageView alloc] initWithImage:bg];
    backgroundView.alpha = 0.7;

    //I add the views to the window here
    [window addSubview:backgroundView];
    [window addSubview:self.view];

    [activityIndicator startAnimating];

    [self fadeIn:self.view];
    }
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 1
    where is your button action code? – Balu Apr 19 '13 at 13:31
  • Also, what kind of object is `self` in this case and what is retaining it? – Phillip Mills Apr 19 '13 at 13:34
  • The button action code never gets called, but at the moment it just has an NSLog statement. 'self' in this case is a ViewConroller – James Elkins Apr 19 '13 at 13:37
  • where you are adding button? – Balu Apr 19 '13 at 13:38
  • The button is added in the interface builder. – James Elkins Apr 19 '13 at 13:40
  • What is the class of deallocated instance ? It seems like you never retain your viewController used to create the button (and probably handle its actions) so my guess is that it is released. – A-Live Apr 19 '13 at 13:58
  • I have a generic 'ViewController' creating my custom 'LoadingAlertView' which is a type of 'ViewController'. The Button is on the 'LoadingAlertView'. How do I retain my custom 'LoadingAlertView' from the generic 'ViewController' while using ARC? I can't just retain anymore. I tried to use the __strong type and that had no effect. – James Elkins Apr 19 '13 at 14:02
  • Shouldn't the View be retained since it's the active view? – James Elkins Apr 19 '13 at 14:25
  • Presumably, the button is connected to an object that implements its action. I'm not clear so far on which kind of object that is and what's intended to keep that object alive so that the button tap has something to handle it. – Phillip Mills Apr 19 '13 at 14:33
  • @PhillipMills The button is created in the XIB of my 'LoadingAlertView' which it nothing more that a 'View Controller' with a view that is smaller that the screen with a label, activity indicator and a button. In 'Loading Alert View' also has an IBACTION that tied to the button via interface builder. I must be wrong in assuming that the 'LoadingAlertView' would keep the button alive since it's current active view with the generic 'viewController' keeping the 'LoadingViewController' alive since it's active. – James Elkins Apr 19 '13 at 14:40
  • A controller will be kept alive if it is explicitly connected to a strong property or is made part of the view controller hierarchy (push, present...). It doesn't get any special consideration because something else is using its view. – Phillip Mills Apr 19 '13 at 14:44
  • @PhillipMills OK, so there is no way to make it interact like the UIAlert? So I can't use 'addSubview' and expect it to responsive. Is there a way I can add the view to the view hierarchy while maintaining the semi-transparent background? – James Elkins Apr 19 '13 at 14:52
  • You can...but note that a UIAlertView doesn't have any implicit dependencies on a controller; it either handles its own actions or farms them out to a delegate you provide. – Phillip Mills Apr 19 '13 at 14:59
  • @PhillipMills That is exactly what I'm trying to do. I want the 'LoadingAlertView' to handle itself and then when the button is pressed send a message to the delegate. How to I display the view while still retaining it so that the thing isn't deallocated. – James Elkins Apr 19 '13 at 15:07
  • That doesn't match something you said earlier: "'LoadingAlertView' which is a type of 'ViewController'". (i.e. view != view controller) – Phillip Mills Apr 19 '13 at 15:17
  • @PhillipMills Then I must be doing something wrong then because that it the type of interaction that I want. So to accomplice what I want I should be using a UIView, not a UIViewController? I thought in order to show a view it needs to be a view controller, forgive me for my ignorance in this matter, it's the first time I've tried to create custom controls. – James Elkins Apr 19 '13 at 15:22

0 Answers0