单触式旋转和navigationViewController

时间:2012-06-10 18:45:26

标签: uinavigationcontroller xamarin.ios rotation

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

window.AddSubview(viewController.NavigationController.View);

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

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

1 个答案:

答案 0 :(得分: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;
    }
}