如何将数据从 View Controller 发送到 View Controller Xamarin iOS(C#)

How send data from View Controller to View Controller Xamarin iOS(C#)

我有一些按钮(名称 - 'button_arrow_product002')的视图,我想发送数据到按钮 'button_arrow_product002' 被点击 我的代码在 ViewController 名称 'HarachieRolliController':

String title = json[1]["post_title"].ToString();
button_arrow_product002.TouchUpInside += (o,s) => {         
        DetailTovarProsmotr x;
        x.HendlerButtonClicked(title);
        Console.Out.WriteLine("Нажали кнопку перехода в детальный просмотр!!!!");
};

代码 ViewCotroller 单击了什么手柄按钮:

namespace murakami_kiev
{
partial class DetailTovarProsmotr : UIViewController
{

    public DetailTovarProsmotr (IntPtr handle) : base (handle)
    {
    }
    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

    }
    public void HendlerButtonClicked(String json){
        titleproduct.Text = json;

    }
}
}

在这段代码中我有错误

'Use of unassigned local variable'

当我创建时 'new DetailTovarProsmotr()' 我肯定有一些争论,但我不知道那些。

更新

我正在创建第二个构造函数并接收字符串类型变量。但是我有错误 'Object reference not set to an instance of an object'。帮我解决我的问题。我的截图:

作为夏天。所有异常的根源是您试图访问/设置未实例化 class.

的成员

正如我在评论中提到的,您需要创建一个实例,然后才能对其进行修改。

要解决您的第一个问题,您需要将 DetailTovarProsmotr x; 更改为 DetailTovarProsmotr x = new DetailTovarProsmotr(); 并在 DetailTovarProsmotr 控制器中创建一个标准构造函数。

这同样适用于你的第二个关于 titleproduct 的问题。 titleproduct 是 UITextView 类型,因此您需要创建一个实例,然后您可以通过设置文本 属性 来对其进行操作。在 titleproduct.Text = title2; 之前添加 titleproduct = new UITextView(new CGRect(10,10,120,30)); 将解决您的 NullReferenceException。

阅读此链接上的帖子和讨论回答我的问题:

https://chat.whosebug.com/rooms/100060/discussion-between-dylan-s-and-artem-shcherbakov