初始化从另一个ViewController接收后的数据 [英] Initialization Data after it receiving from another ViewController

查看:262
本文介绍了初始化从另一个ViewController接收后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班的ViewController。

I have two ViewController classes.

第一 - HarachieRolliController。
二 - DetailTovarProsmotr

First - HarachieRolliController. Second - DetailTovarProsmotr

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

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

下面

单击以button_arrow_product002后:

After clicking to button_arrow_product002 :

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

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.

第一类代码:

    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中梅托德运行

  1. ViewDidLoad metod is run

点击按钮(button_arrow_product002)

Clicked Button (button_arrow_product002)

构造DetailTovarProsmotr运行

Constructor DetailTovarProsmotr is run

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

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

ГорячийроллслососемиугремHendlerButtonClicked梅托德运行

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

我认为数据后通过了的ViewController开始工作。而且因为这titleproduct001.Text =称号;没有作品。我已经警告作为结果。

牌九我能解决这个问题?

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. Gow I can fix this?

推荐答案

我想我还不如寄这与参考代码中的答案。

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

的问题是,你没有正确使用故事板。 UIViewController的实例之间的转换需要通过塞格斯发生。

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

现在,你可能想知道...我如何通过DetailTovarPostr标题字符串?当使用一个故事板,您不能使用构造函数的参数来传递数据,但你可以使用塞格斯!

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有一个PrepareForSegue方法,该方法将不正是你想要的。

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

在你使用SEGUE,那么,iOS将实例化的UIViewController(调用new ),并与所有的其意见(如titleproduct001)初始化。这种观点是NULL,因为UIViewController的不正确初始化。

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.

您说您已经拥有了视图控制器之间界定的SEGUE。如果你需要一些帮助获取/设置ID,然后让我知道,我会后这一点。

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.

这篇关于初始化从另一个ViewController接收后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆