0

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

在.h中:

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

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

以 .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

以 .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)sender 动作。

4

1 回答 1

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

于 2012-10-10T08:06:52.010 回答