从另一个 ViewController 接收后的初始化数据

Initialization Data after it receiving from another ViewController

我有两个 ViewController class。

首先 - HarachieRolliController。 第二 - DetailTovarProsmotr

我在 Xamarin 的 MainStoryboard 文件中创建了它 iOS (C#)

此处截图

点击后 button_arrow_product002 :

1) 需要打开DetailTovarProsmotr 2) 需要传数据给DetailTovarProsmotr,显示在一些字段中,比如(titleproduct001),这是string.

第一个class代码:

    partial class HarachieRolliController : UIViewController
{

    public HarachieRolliController (IntPtr handle) : base (handle)
    {
    }
    public async override void ViewDidLoad ()
    {

        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
        JsonValue json = await FetchAsync(url2);

    ParseAndDisplay (json);
    }

    private async void ParseAndDisplay(JsonValue json)
    {

        String title = json[1]["post_title"].ToString();
        button_arrow_product002.TouchUpInside += delegate {

            Console.Out.WriteLine ("Clicked Button (button_arrow_product002)");
            DetailTovarProsmotr myvarible = new DetailTovarProsmotr (title);

        };
    }
}
}

第二个class代码:

    public partial class DetailTovarProsmotr : UIViewController
{
    public string mytitle;

    public DetailTovarProsmotr (IntPtr handle) : base (handle)
    {

    }
    public DetailTovarProsmotr(String title){
        this.mytitle = title;
        Console.Out.WriteLine ("Constructor DetailTovarProsmotr is run");
        Console.Out.WriteLine (mytitle+" Console.Out.WriteLine (mytitle)");
        HendlerButtonClicked (mytitle);

    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        Console.Out.WriteLine (mytitle+" ViewDidLoad metod is run");

    }

    public void HendlerButtonClicked(String title){
        Console.Out.WriteLine (title+" HendlerButtonClicked metod is run");
        titleproduct001.Text = title;

    }
}

在 classes 和方法中,我有控制台显示,以查看我的应用程序的工作阶段。控制台日志:

  1. ViewDidLoad 方法是 运行

  2. 单击了按钮 (button_arrow_product002)

  3. 构造函数 DetailTovarProsmotr 是 运行

  4. "Горячий ролл с лососем и угрем" Console.Out.WriteLine(我的标题)

  5. " Горячий ролл с лососем и угрем " HendlerButtonClicked 方法是 运行

我认为数据通过后 ViewController 开始工作。也因为这个 titleproduct001.Text = title;不起作用。结果我有警告。 我可以解决这个问题吗?

我想我也可以post这是一个答案,带有代码供参考。

问题是您没有正确使用 Storyboard。 UIViewController 实例之间的转换需要通过 segues 发生。

现在您可能想知道...如何将标题字符串传递给 DetailTovarPostr?使用 Storyboard 时,不能使用构造函数参数传递数据,但可以使用 segues!

UIViewController 有一个 PrepareForSegue 方法,它会完全按照你的要求去做。

  partial class HarachieRolliController : UIViewController
  {

    public HarachieRolliController (IntPtr handle) : base (handle)
    {
    }
    public async override void ViewDidLoad ()
    {

        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
        JsonValue json = await FetchAsync(url2);

        ParseAndDisplay (json);
    }

    private async void ParseAndDisplay(JsonValue json)
    {

        String title = json[1]["post_title"].ToString();
        button_arrow_product002.TouchUpInside += delegate {

            Console.Out.WriteLine ("Clicked Button (button_arrow_product002)");
            // Use a segue to display the DetailTovarProsmotr
            this.PerformSegue('YOUR SEGUE ID', this);
            // The segue needs to be defined in the storyboard with a unique id

        };
    }

    protected override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender) {
        var detailTovarPostr = segue.DestinationViewController as DetailTovarPostr;
        // Initialized your custom view controller however you like
       // Consider defining properties or an initialize method and pass in the title and other data
       // TODO pass data :)
    }
}

使用 segue 后,iOS 将实例化 UIViewController(调用 new)并使用其所有视图(如 titleproduct001)对其进行初始化。该视图为 NULL,因为 UIViewController 未正确初始化。

你说你已经在视图控制器之间定义了一个 segue。如果您需要一些帮助 getting/setting ID,请告诉我,我也会 post。