iphone - 使用pushViewController时UINavigationController变成null

标签 iphone ios uikit

我在使用 UINavigationController 时遇到了一些问题.

我有第一个ViewController (called LaunchViewController)使用导航 Controller (工作正常),我可以使用 pushViewController显示second ViewController (called SettingViewController)它也工作得很好,但是当(从第一个 View Controller )我想显示第三个 ViewController (called IPhonePage), 时显示 View 后,当我想使用 navigation controller 时在我的IPhonePagenavigation controllernull...

不知道我的问题是否清楚,我可以向您展示我用来执行此操作的代码:

AppDelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    LaunchViewController* controller = [[LaunchViewController alloc] initWithNibName:@"LaunchViewController" bundle:nil];
    _navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    _navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.window setRootViewController:_navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

LaunchViewController.m :

- (IBAction)showSettings:(id)sender {
    SettingViewController* controller = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

这工作正常(我可以访问 navigation controller 中的 SettingViewController)

但是这个,

LaunchViewController.m :

- (void)ShowPageWithIMDevicePage:(id<IMDevicePage>)page {
    [self.navigationController pushViewController:(UIViewController*)page animated:YES];
}

页面创建者:

- (id<IMDevicePage>)CreatePage {
    return [[IPhonePage alloc] init];
}

我的 iPhonePage 类看起来像这样,

iPhonePage.h:

@interface IPhonePage : UIViewController <IMDevicePage>

iPhonePage.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"navigation controller = %@", self.navigationController);
}

日志显示导航 Controller = (null),例如我无权访问navigation controller.

我真的希望有人能理解我想要什么,我知道这有点复杂,但我这样做是有特定原因的。

谢谢!干杯:)

编辑 实际上,这是因为我创建了页面,并且设置了不同的内容,例如标题、内容和工具栏,然后我调用 pushViewController ,奇怪的是我有一个方法来设置UIViewController的内容

IPhonePage.m

- (void)SetContentWithIMDeviceControl:(id<IMDeviceControl>)content {
    self.view = (UIView*)content;
}

之后它不再调用我的 viewDidLoad 方法(有任何线索吗?)。 我还有一个方法,其作用是在 View 底部添加工具栏

- (void)SetApplicationBarWithIMDeviceApplicationBar:(id<IMDeviceApplicationBar>)appBar {
}

一开始我想用导航 Controller 来设置这个工具栏,但是因为这个方法是在pushViewController之前调用的。方法中,导航 Controller 为零。 是否有可能在不调用 pushViewController 的情况下手动设置导航 Controller ? ?或者有其他方式添加工具栏吗?就像声明一个工具栏并将其添加为 subview 一样?

最佳答案

尝试类似:

- (void)ShowPageWithIMDevicePage:(id<IMDevicePage>)page {
    YourDelegate* delegate = (YourDelegate*)[[UIApplication sharedApplication] delegate];
    IPhonePage* pageVC = (IPhonePage*)page;
    [delegate.navigationController pushViewController:pageVC animated:YES];
}

关于iphone - 使用pushViewController时UINavigationController变成null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13363305/

相关文章:

c - 使用属性语法设置 shadowColor 会产生编译器错误 : Expected identifier before '[' token

iphone - setEnabled 不适用于 UIButton

ios - 在同一个 POST 中发送 NSData 和 NSString

ios - 真实 iPhone 上的 rn-fetch-blob 无法上传图像(本地主机测试)

iphone - UIViewController viewDidLoad方法中设置UITableView的contentOffset不生效

iphone - facebook uri 方案在 iOS6 上不再起作用吗?

ios - UIButton 从错误的类调用函数

ios - Apple Watch/WatchKit 语音合成器

iPhone - NSDateComponents/NSCalendar 和耗时(例如,3 小时前扫描。)

ios - UILabel 显示 1k 表示 int 值 1000 和 2k 表示 2000 等等