Initialization Data after it receiving from anothe

2019-07-12 21:29发布

I have two ViewController classes.

First - HarachieRolliController. Second - DetailTovarProsmotr

I created it in MainStoryboard file in Xamarin iOS (C#)

Here screenshotenter image description here

After clicking to button_arrow_product002 :

1) It needs to open DetailTovarProsmotr 2) It need to pass data to DetailTovarProsmotr and display it in some fields , for example (titleproduct001), this is string.

First class code:

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

        };
    }
}
}

Second class code:

    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;

    }
}

In classes and methods I have Console displaying, to see stages of working in my app. Console log:

  1. ViewDidLoad metod is run

  2. Clicked Button (button_arrow_product002)

  3. Constructor DetailTovarProsmotr is run

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

  5. " Горячий ролл с лососем и угрем " HendlerButtonClicked metod is run

I think that data passes later that ViewController start to work. And because of this titleproduct001.Text = title; doesn't works. I have warning as result. enter image description here Gow I can fix this?

1条回答
再贱就再见
2楼-- · 2019-07-12 22:24

I figured I might as well post this as an answer with the code for reference.

The problem is that you're not using the Storyboard correctly. Transitions between UIViewController instances needs to occur through segues.

Now you might be wondering... How do I pass the DetailTovarPostr the title string? When using a Storyboard, you can't use constructor parameters to pass data, but you can use segues!

UIViewController has a PrepareForSegue method which will do exactly what you want.

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

Once you use a segue, then iOS will instantiate the UIViewController (call new) AND initialize it with all of its views (like the titleproduct001). That view is NULL because the UIViewController was not properly initialized.

You say that you already have a segue defined between the view controllers. If you need some help getting/setting the ID, then let me know and I'll post that as well.

查看更多
登录 后发表回答