Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KnightWRX

macrumors Pentium
Original poster
Jan 28, 2009
15,046
4
Quebec, Canada
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 ?
 
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 ?
 
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 :)
 
Last edited:
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];
 
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];

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.