无法在Xamarin.IOS中查看PushViewController

时间:2016-06-17 05:28:37

标签: xamarin uiwebview xamarin.ios

ViewControllers中只有两个First ViewController只有按钮touchupinside上的按钮我正在尝试打开Second ViewController。第二个ViewController有webView。从touchupinside我传递了网址。但在这里,我无法看到Second ViewController,也无法看到webView。

在UI按钮=>拖动Action Segue(Show)

FirstViewController代码:

UINavigationController _nav = null;
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    _nav = new UINavigationController();
}
partial Void BtnOne_TouchUpInside(UIButton sender)
{
    _nav.PushViewController(new CustomeWebViewController("http://www.google.com"), true);
}

SecondViewController代码:

string _url;
public CustomeWebViewController(string url)
{
    _url = url;
}
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    var webView = new UIWebView(View.Bounds);
    webView.LoadRequest(new NSUrlRequest(new NSUrl(_url)));
    webView.ScalesPageToFit = true;
    View.AddSubview(webView);
}

1 个答案:

答案 0 :(得分:1)

所以问题是你没有有效的NavigationController。你必须有一个导航。

首先要做的是向NavigationController添加Storyboard

要执行此操作,请按照instructions 3,4,5,6 e 7。

但基本上你会添加NavigationController,并指出你的FirstViewController

现在添加FirstViewController指向SecondViewController

FirstViewController的代码中,您必须执行此操作:

partial void BtnOne_TouchUpInside(UIButton sender)
{
    NavigationController.PushViewController(new secondviewcontrller("https://xamarin.com"), true);
}

结果:

enter image description here enter image description here

替代

注意:从ViewControllers实例化Storyboard是一种更好的做法,因此您可以这样做:

partial void BtnOne_TouchUpInside(UIButton sender)
{

    var other = Storyboard.InstantiateViewController("secondviewcontroller") as secondviewcontrller;
    other.SetUrl("https://xamarin.com");

    NavigationController.PushViewController(other, true);
}

然后在SecondViewContrller

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    webView = new UIWebView(View.Bounds);
    View.AddSubview(webView);
    webView.ScalesPageToFit = true;
    webView.LoadRequest(new NSUrlRequest(new NSUrl(_url)));
}

public void SetUrl(string url) 
    _url = url;
}