开发者

Pushing and Popping ViewControllers using a Navigation Controller: Implementation

Like many others I started to code an experiment today where I would have two view controllers and be able to switch between them. I got this to work using a navigation controller, but I have a question about the implementation.

In my TwoViewsAppDelegate, I define the navigation controller and the rootViewController.

@interface TwoViewsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController *rootViewController;
}

and set them up as follows:

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    rootViewController = [[RootViewController alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

Then in my rootViewController, I define th开发者_如何学Goe level2ViewController that I am going to switch to, and a button that I'm going to press to make the switch happen:

@interface RootViewController : UIViewController {

    UIButton *theButton;
    Level2ViewController *level2ViewController;
}

Here's the response to the button being pressed in RootViewController.m:

-(void)level1ButtonPressed:(id)sender
{
    if (level2ViewController == nil)
    {
        level2ViewController = [[Level2ViewController alloc] init];
    }

    [self.navigationController pushViewController:level2ViewController animated:YES];
}

The problem is that if there was going to be a level3ViewController, it would have to be defined as a member of level2ViewController, etc. for however many view controllers i wanted to push onto the stack.

It would be nice to be able to define all the view controllers in one place, preferably the app Delegate. Is this possible?


To solve this, you can create a callback-type method which uses the delegate of the class that'll be sending the requests for the view controllers. Best explained through code...

RootViewController.h

#import "RootInterfaceView.h"
// all the other VC imports here too
@interface RootViewController : UIViewController <RootInterfaceViewDelegate>
{
    RootInterfaceView *interface;
}

RootViewController.m

-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag
{
    switch (itemTag)
    // then create the matching view controller
}

RootInterfaceView.h

// imports here if required
@protocol RootInterfaceViewDelegate;

@interface RootInterfaceView : UIView <RootInterfaceItemViewDelegate>
{
    id <RootInterfaceViewDelegate> delegate;
}

@property (nonatomic, assign) id delegate;

@end

@protocol RootInterfaceViewDelegate <NSObject>

@optional
-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag;

@end

RootInterfaceView.m

// remember to synthesize the delegate
-(void)rootInterfaceItemSelected: (RootInterfaceItemView*)rootInterfaceItemView
{
    NSUInteger theTag = rootInterfaceItemView.tag;
    if ([self.delegate respondsToSelector:@selector(rootInterfaceView:didSelectItem:)])
        [self.delegate rootInterfaceView:self didSelectItem:theTag];
}

Alternatively, if the only options from level 2 were either back to root/pop one VC or to push controller 3, then it'd be fine for level 2 to be importing 3 to allow for it's creation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜