0

如何覆盖 UINavigationController 的构造函数以传入 rootViewController?

我将在 Objective-C 中使用如下方法:

-(id)initWithRootViewController:(UIViewController*)rootViewController
{
    UIViewController *fakeController = [[[UIViewController alloc] init] autorelease];
    if (self = [super initWithRootViewController:fakeController]) {

      self.fakeRootViewController = fakeController;

      rootViewController.navigationItem.hidesBackButton = YES;

      [self pushViewController:rootViewController animated:NO];
    } 
    return self;
}

先感谢您。问候。

PS 这段代码取自Change the root view controller

编辑:

谢谢您的回复。我对前面的代码片段很感兴趣,因为它特别有趣。

@Geoff Norton:也许我永远不会使用您的解决方案,但无论如何我觉得它很棒......

我的尝试是创建一种 UINavigationViewController 作为模板。特别是,UINavigationController 最初有一个 loginView(它可能是一种 rootviewcontroller)。然后在登录时,我可以有两种视图:主视图和辅助视图。前者与登录视图处于同一级别(它们可能是一种根视图控制器)。后者被推到第一个之上。您可以通过普通 UInavigationController 堆栈或通过工具栏进行导航。工具栏仅加载主视图。

是否可以使用 UINavigationController 来做到这一点?

再次感谢你。问候。

4

2 回答 2

4

它可能,但你不应该这样做。根据 Apple 的说法,UINavigationController “不是为子类化而设计的”。 如果你坚持这样做:

public YourNavController : UINavigationController {
    [Export ("initWithRootViewController:")]
    public YourNavController (UIViewController vc) {
      UIViewController fc = new UIViewController ();
      Handle = Messaging.intptr_objc_msgSend_intptr (this.Handle, Selector.GetHandle ("initWithRootViewController:"), fc.Handle);
      FakeRootViewController = fc;
      vc.NavigationItem.HidesBackButton = true;
      PushViewController (vc, false);
    }
}

接近的东西应该工作。

于 2011-04-20T18:21:17.343 回答
2

就像 Geoff Norton 指出的那样,你不应该继承 UINavigationController 的子类。

我自己坚持了几次,只是为了发现有一些错误,每隔一段时间就会弹出一些没有逻辑解释的错误。当你用谷歌搜索时,答案总是“你不应该继承 UINavigationController”。

于 2011-04-21T13:44:58.550 回答