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

标签 c# ios xamarin xamarin.ios

我有两个 ViewController 类。

首先 - HarachieRolliController。 第二 - DetailTovarProsmotr

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

此处截图enter image description here

点击 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;

    }
}

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

  1. ViewDidLoad 方法运行

  2. 点击按钮 (button_arrow_product002)

  3. 运行构造函数 DetailTovarProsmotr

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

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

我认为数据在 ViewController 开始工作之后传递。因为这个 titleproduct001.Text = title;不起作用。结果我有警告。 enter image description here 我可以解决这个问题吗?

最佳答案

我想我不妨将此作为答案与代码一起发布以供引用。

问题是您没有正确使用 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)并使用其所有 View (如 titleproduct001)对其进行初始化。该 View 为 NULL,因为 UIViewController 未正确初始化。

您说您已经在 View Controller 之间定义了一个 segue。如果您在获取/设置 ID 方面需要帮助,请告诉我,我也会发布。

关于c# - 从另一个 ViewController 接收后的初始化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34633235/

相关文章:

c# - caliburn.micro 导体不工作

ios - GPPSignIn 共享实例 -> EXC_BAD_ACCESS(代码 = EXC_I386_GPFLT)

xamarin - 制作一个 2 单元格的水平堆栈布局,占设备屏幕宽度的 50%

c# - lambda 表达式中的子句

C# 复制正在写入的文件

ios - 为什么两个 CGRect 在视觉上不相交?

ios - 初学者核心数据和关系

xamarin - ListView 在 xamarin.forms 中水平和垂直滚动

c# - 如何强制 XAMARIN 应用仅使用 WIFI

c# - Convert.ToString 不处理 null