如何创建UINavigationController的实例?

时间:2012-10-10 08:00:55

标签: objective-c uinavigationcontroller single-instance appdelegate

在我的第一个iPhone应用程序中,我有一个NavigationController。 如何在AppDelegate中定义UINavigationController的实例并为我的默认导航控制器设置它?

在.h:

@interface DefaultTableAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
    //...
    UINavigationController *myNavigationController;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) UINavigationController *myNavigationController;
//...
@end

in .m:

#import "DefaultTableAppDelegate.h"
#import "SHKConfiguration.h"
#import "SKCustomConfigurator.h"
#import "DefaultTableViewController.h"

@implementation DefaultTableAppDelegate 

@synthesize window = _window;
@synthesize myNavigationController = _myNavigationController;
//...

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

    DefaultTableViewController *main = [[DefaultTableViewController alloc]init];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    myNavigationController = [[UINavigationController alloc]initWithRootViewController:main];

    myNavigationController.navigationBar.hidden = YES;
    [self.window addSubview:myNavigationController.view];

    self.window.rootViewController=myNavigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

UINavigationItem的CustomNavigationItem子类:

在.h:

#import <UIKit/UIKit.h>
#import "DefaultTableAppDelegate.h"

@interface CustomNavigationItem : UINavigationItem
{
    //...
    DefaultTableAppDelegate *myDelegate;
} 
@end

in .m:

#import "CustomNavigationItem.h"
#import "DefaultTableViewController.h"

@implementation CustomNavigationItem
//...

-(IBAction)actionApply:(id)sender
{
    myDelegate = [[UIApplication sharedApplication] delegate];
    //...
    [myDelegate.myNavigationController popViewControllerAnimated:YES];  
}

@end

以下是我的故事板的截图:http://postimage.org/image/sv6elwmcz/

TabBarController的NavigationItem的类设置为CustomNavigationItem,NavigationItem的右键具有 - (IBAction)actionApply:(id)发送者动作。

1 个答案:

答案 0 :(得分:0)

试试这个:

#import "AppDelegate.h"
#import "MainViewController.h" //Or whatever you named your viewController

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   MainViewController *main = [[MainViewController alloc]init];

   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];// Override point for customization after application launch.
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:main];

nav.navigationBar.hidden = YES;
 [self.window addSubview:nav.view];

self.window.rootViewController=nav; 
[self.window makeKeyAndVisible];


return YES;

}

修改 不使用appDelegate 这样做:

-(IBAction)actionApply:(id)sender
{
  [self.navigationController popViewControllerAnimated:YES];
}

并从UINavigationController *myNavigationController;

中删除appDelegate.h