【问题标题】:How can I make an instance of UINavigationController?如何创建 UINavigationController 的实例?
【发布时间】:2012-10-10 08:00:55
【问题描述】:

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

【问题讨论】:

    标签: objective-c uinavigationcontroller single-instance appdelegate


    【解决方案1】:

    试试这个:

    #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];
    }
    

    并从您的appDelegate.h 中删除UINavigationController *myNavigationController;

    【讨论】:

    • 感谢您提供此代码,它看起来不错,但我的应用程序无法使用它。启动画面后出现一个空的黑屏并且什么都不做......我正在使用最新的 Xcode、ARC,并且默认导航控制器是使用情节提要创建的。 MainViewController 是分配给第一个视图的类,在哪里“启动”导航控制器,对吧?
    • 谢谢,但您忘记了 CustomNavigationItem 是 UINavigationItem 的子类,所以我的操作中没有 self.navigationController。这就是问题...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2013-05-08
    相关资源
    最近更新 更多