From viewController back to AppDelegate

Discussion in 'iOS Programming' started by KnightWRX, Nov 3, 2010.

  1. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #1
    Quick question. I have some code I consider a bit dirty. Basically, in the AppDelegate, I embed my initial view and do some animation from there (call NSTimer at the end of loadView). At this point, before the timer fires, the AppDelegate continues processing code in applicationDidFinishLaunching.

    Now, ultimately, once the animation is done with, I destroy this view and load my main view. The problem I have is that I do it from this viewController. I'd want to do it from the AppDelegate instead.

    Confusing uh ? Here's a snippet of code to make it clearer :

    Code:
    - (void)loadView {
    ...
    [self.view addSubview:Animation];
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(doAnimate) userInfo:nil repeats:NO];
    ...
    }
    
    -(void)doAnimate { 
    ...
    [UIView setAnimationDidStopSelector:@selector(stopAnimate)];
    ...
    }
    
    -(void) stopAnimate {
    	[Animation removeFromSuperview];
    	[Animation release];
    ... (boring new view allocation) ...
            [self.view addSubview:[viewController view]];
    }
    
    Right now, NSTimer executes the selector I gave it, which in turn calls a second selector for the end of the animation. I then load the main view from that selector, resulting in App -> View -> View.

    I'd want to somehow signal to AppDelegate that this viewcontroller is done so it can react and allocate a new view based on another controller.

    Is there a way to do this ?
     
  2. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #2
    I would use a delegate from the view controller to call the AppDelegate. This can be the signal you mentioned on your post.
     
  3. KnightWRX thread starter macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    I've used delegates in the past for things like NSXMLParser, but not quite in the manner you speak of. I read all the chapters on View controller lifecycles and haven't seen this.

    Do you have some quick code to show me that would explain this ?
     
  4. ritsard, Nov 4, 2010
    Last edited: Nov 4, 2010

    ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #4
    If I understand what you would like to do correctly, you want to have the AppDelegate handle the view switching and not having to have the (animation) viewController call the next view. If this is correct, what I suggest is to have a delegate property in the viewController. Once you initialize the view controller in the AppDelegate you can set the viewController's delegate to the AppDelegate object.

    Code:
    ViewController.h
    @interface ViewController : NSObject
    {
           id delegate;
    }
    
    @property id delegate;
    
    ViewContrller.m
    - (void)willMoveToSuperview:(UIView *)newSuperview
    {
       // assuming that when you remove this view from the main view this method gets called and the UIView is nil
       if(newSuperView == nil)
       {
            if([delegate respondsToSelector:@selector(operation)])
               [delegate operation];
       }
    }
    
    AppDelegate.m
    
    - (void) loadView
    {
       [Animation setDelegate:self];
       ......
    }
    
    - (void) operation
    {
       [Animation removeFromSuperview];
    }
    
    something like that. If this is not what you wanted, please elaborate on what you are trying to achieve :)
     
  5. KnightWRX thread starter macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #5
    I'll give that a try, seems pretty much exactly like what I want.
     
  6. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #6
    AppDelegate

    in order to access u'r AppDelegate from somewhere in the project u call:
    [[UIApplication sharedApplication] delegate]
    the method returns object of type id so u need casting to u'r specific AppDelegate class which is something like:
    ProjectNameAppDelegate *appDelegate =
    (ProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
     
  7. KnightWRX thread starter macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #7
    Really ? Sounds simpler than what I ended up doing, which is implementing a protocol that specified a DidFinish method and just made the AppDelegate implement that protocol.

    A simple setDelegate when creating the viewController then glued everything and it sort of works like a charm (I still have to track down a BAD_ACCESS error when I leave the Window without a view or viewcontroller, but that should never happen anyhow).

    Maybe I'll take a look at your method later on, I had moved on to writing the XML parser for the configuration files I wanted to use.
     

Share This Page