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 に答える