【问题标题】:How send data from View Controller to View Controller Xamarin iOS(C#)如何将数据从 View Controller 发送到 View Controller Xamarin iOS(C#)
【发布时间】:2016-01-18 18:34:46
【问题描述】:

我有一些按钮的视图(名称 - 'button_arrow_product002'),我想发送数据 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("Нажали кнопку перехода в детальный просмотр!!!!");
};

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

    }
}
}

在这段代码中我有错误

'使用未赋值的局部变量'

当我创建“new DetailTovarProsmotr()”时,我有一些论据,但我不知道。

更新

我正在创建第二个构造函数并接收字符串类型变量。但我有错误'对象引用未设置为对象的实例'。帮我解决我的问题。我的截图:

【问题讨论】:

  • 您的 DetailTovarProsmotr 控制器未初始化。更改 DetailTovarProsmotr x;到 DetailTovarProsmotr x = new DetailTovarProsmotr();它应该可以工作。
  • 'DetailTovarProsmotr x = new DetailTovarProsmotr();'当我创建“new DetailTovarProsmotr()”时,我有一些论据,但我不知道这些。错误:'类型 murakami_kiev.DetailTovarProsmotr' does not contain a constructor that takes 0' 参数'
  • 向控制器添加第二个构造函数,如下所示:public DetailTovarProsmotr(){}
  • 感谢您的回答。我正在更新我的帖子
  • titleproduct是什么类型?

标签: c# ios xamarin xamarin.ios


【解决方案1】:

作为一个夏天。所有异常的根源是您试图访问/设置未实例化类的成员。

正如我在 cmets 中已经提到的,您需要创建一个实例,然后您可以对其进行修改。

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

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

【讨论】:

  • 我正在尝试所有这些,但是这个 - 'titleproduct = new UITextView(new CGRect(10,10,120,30));'覆盖我在 Main.storyboard 文件中绘制的 titleproduct。我发布详细问题stackoverflow.com/questions/34633235/…
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 2020-02-17
相关资源
最近更新 更多