我有两个ViewController类。

首先-HarachieRolliController。
第二-DetailTovarProsmotr

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

截图c# - 从另一个ViewController接收到的初始化数据-LMLPHP

单击到button_arrow_product002之后:

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

头等舱代码:

    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;

    }
}

在类和方法中,我将显示控制台,以查看在我的应用程序中工作的各个阶段。控制台日志:
  • ViewDidLoad方法运行
  • 单击的按钮(button_arrow_product002)
  • 构造函数DetailTovarProsmotr运行
  • “Горячийроллслососемиугрем” Console.Out.WriteLine(mytitle)
  • “Горячийроллслососемиугрем” HendlerButtonClicked方法运行

  • 我认为ViewController开始工作之后,数据就会通过。并由于这个标题product001.Text = title;不起作用。结果有警告。
    c# - 从另一个ViewController接收到的初始化数据-LMLPHP
    天哪,我能解决这个问题吗?

    最佳答案

    我认为我不妨将此作为答案附有代码以供参考。

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

    现在您可能想知道...我如何传递DetailTovarPostr标题字符串?使用情节提要时,不能使用构造函数参数来传递数据,但可以使用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。

    您说您已经在视图控制器之间定义了序列。如果您在获取/设置ID方面需要帮助,请告诉我,我也将其发布。

    10-08 07:30
    查看更多