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

我有两个ViewController类。

首先 – HarachieRolliController。 Second – DetailTovarProsmotr

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

这里截图 在这里输入图像说明

点击button_arrow_product002后:

1)需要打开DetailTovarProsmotr 2)需要将数据传递给DetailTovarProsmotr并显示在一些字段中,例如(titleproduct001),这是string。

一stream的代码:

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); }; } } } 

二级代码:

  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; } } 

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

  1. ViewDidLoad metod运行

  2. 点击button(button_arrow_product002)

  3. 构造函数DetailTovarProsmotr运行

  4. “Горячийроллслососемиугрем”Console.Out.WriteLine(mytitle)

  5. “Горячийроллслососемиугрем”HendlerButtonClicked metod运行

我认为数据通过后,ViewController开始工作。 并因为这个titleproduct001.Text = title; 不起作用。 我有警告的结果。 在这里输入图像说明 Gow我可以解决这个问题吗?

我想我不妨把这个post作为参考代码的答案。

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

现在你可能想知道…如何通过DetailTovarPostr标题string? 使用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。 如果您需要一些帮助获取/设置ID,然后让我知道,我也会发布。