【问题标题】:Calling a non-static function of a UIViewController class调用 UIViewController 类的非静态函数
【发布时间】:2018-05-26 15:14:19
【问题描述】:

我正在尝试使用 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();
    }
}
}

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

有什么想法吗?

【问题讨论】:

    标签: c# ios xcode uiviewcontroller


    【解决方案1】:

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

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

    很高兴它有效。这个没有工作的原因是'SetLink'没有在UIViewController类上定义,这是你之前将'ctrl'转换成的类型。旁注:在这种情况下,实际上可以动态调用它,但这对您的情况来说是不必要的复杂化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      相关资源
      最近更新 更多