在我的第一个 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 动作。