1

我是 monotouch 的新手,使用 NavigationViewController 时遇到问题:在 FinishedLauching 方法中,如果我评论这一行:

window.AddSubview(viewController.NavigationController.View);

窗口旋转没有问题,但导航控制器不起作用。如果此行在代码中(不是注释),则 navigationController 可以工作,但屏幕不会旋转。

任何人都知道如何解决这个问题?

4

1 回答 1

0

设置 UIWindow 的 RootViewController 属性是使用导航控制器的正确方法。

MyViewCtrl ctrl1 = new MyViewCtrl ("One");
_nav = new UINavigationController (ctrl1);

window.RootViewController = _nav;

也不要忘记覆盖控制器中的 ShouldAutorotateToInterfaceOrientation 方法。这是一个完整的样本

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow window;
    UINavigationController _nav;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);

        MyViewCtrl ctrl1 = new MyViewCtrl ("One");
        _nav = new UINavigationController (ctrl1);

        window.RootViewController = _nav;
        window.MakeKeyAndVisible ();

        return true;
    }
}

public class MyViewCtrl : UIViewController
{
    public MyViewCtrl (string title)
    {
        this.Title = title;
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        this.View.BackgroundColor = UIColor.White;

        if (this.Title.Equals ("One"))
        {
            UIBarButtonItem testButton = 
                new UIBarButtonItem ("Two", UIBarButtonItemStyle.Bordered, 
                                     (object sender, EventArgs e) => {
                    this.NavigationController.PushViewController (
                        new MyViewCtrl ("Two"), true);
                } );
            this.NavigationItem.RightBarButtonItem = testButton;    
        }
    }   

    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
        return true;
    }
}
于 2012-06-11T02:27:11.523 回答