调用UIViewController类的非静态函数

时间:2018-05-26 15:14:20

标签: c# ios xcode uiviewcontroller

我正在尝试用c#构建一个移动的跨平台ios应用程序,此时我陷入困境,因为我无法调用我的UIViewController的非静态函数。 这是我的Main.cs:

...
namespace BSoft.iOS
{
public class Application 
{
    static void Main(string[] args)
    {
        UIApplication.Main(args, null, "AppDelegate");
    }

    public static void OpenWebPage(string link, UIViewController controller)
    {
        UIStoryboard board = UIStoryboard.FromName("Main", null);
        UIViewController ctrl = (UIViewController)board.InstantiateViewController("WebViewController");
        /// here I should call SetLink() from WebViewController class
        ctrl.ModalTransitionStyle = UIModalTransitionStyle.PartialCurl;
        controller.PresentViewController(ctrl, true, null);
    }

}
}

这是WebViewController类:

...
namespace BSoft.iOS
{
public partial class WebViewController : UIViewController
{
    WKWebView WebViewer;

    public WebViewController(IntPtr handle) : base(handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        WebViewer = new WKWebView(View.Frame, new WebKit.WKWebViewConfiguration());
        WebViewer.SizeToFit();
        WebViewer.LoadRequest(Foundation.NSUrlRequest.FromUrl(Foundation.NSUrl.FromString("https://www.360bsoft.com/register")));
        WebViewerContainer.AddSubview(WebViewer);
    }

    public void SetLink(string link)
    {
        if(WebViewer!=null)
            WebViewer.LoadRequest(Foundation.NSUrlRequest.FromUrl(Foundation.NSUrl.FromString(link)));
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
    }
}
}

在我的故事板中,我将我的webview ViewController归结为一个自定义类(WebViewController)和一个Identity(WebViewController),问题是当我更改视图时,我无法从主应用程序调用SetLink。

任何想法?

1 个答案:

答案 0 :(得分:1)

在main.cs中,尝试将ctrl转换为WebViewController而不是UIViewController:

WebViewController ctrl = (WebViewController)board.InstantiateViewController("WebViewController");

很高兴它有效。这个工作的原因是UIViewController类没有定义'SetLink',这是你之前投射'ctrl'的类型。旁注:在这种情况下,实际上可以动态地调用它,但这对你的情况来说是不必要的并发症。