2011-04-20 48 views
0

如何重写UINavigationController的构造函数以传入rootViewController?Monotouch:UINavigationController,覆盖initWithRootViewController

我会像在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; 
} 

预先感谢您。问候。

P.S的这段代码已采取从Change the root view controller

编辑:

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

@Geoff诺顿:也许我会永远不可能用你的解决方案,但我觉得很神奇呢?

我的尝试是创造一种UINavigationViewController,它充当一个模板。尤其是,UINavigationController最初有一个loginView(它可能是一种rootviewcontroller)。然后登录时,我可以有两种类型的视图:主视图和次视图。前者与登录视图处于同一级别(它们可能是一种rootview控制器)。后者被推到第一个以上。您可以浏览普通的UInavigationController堆栈或工具栏。工具栏只加载主视图。

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

再次感谢您。问候。

回答

4

它可能,但你不应该这样做。据苹果公司的UINavigationController的是"not designed for subclassing".如果你坚持这样的话:

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); 
    } 
} 

一些接近,应该工作。

2

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

我一直坚持这样做几次,只是为了发现有一些偶尔会出现的错误,没有合理的解释。当你谷歌的时候,答案总是“你应该没有子类UINavigationController”。