我有一些按钮(名称 - 'button_arrow_product002')的 View ,我想发送数据 wen 按钮 '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()”时,我肯定有一些参数,但我不知道那些。
更新
我正在创建第二个构造函数并接收 String 类型变量。但是我有错误“对象引用未设置为对象的实例”。帮我解决我的问题。我的截图:
最佳答案
作为一个夏天。所有异常的根源是您试图访问/设置未实例化类的成员。
正如我在评论中提到的,您需要创建一个实例,然后才能修改它。
要解决您的第一个问题,您需要将 DetailTovarProsmotr x;
更改为 DetailTovarProsmotr x = new DetailTovarProsmotr();
并在 DetailTovarProsmotr Controller 中创建标准构造函数。
这同样基本上适用于您的第二个问题与 titleproduct。 titleproduct 属于 UITextView 类型,因此您需要创建一个实例,然后您可以通过设置 text 属性对其进行操作。在 titleproduct.Text = title2;
之前添加 titleproduct = new UITextView(new CGRect(10,10,120,30));
将解决您的 NullReferenceException。
关于c# - 如何将数据从 View Controller 发送到 View Controller Xamarin iOS(C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34590155/