【问题标题】:Losing data when pass from UIViewController to another class从 UIViewController 传递到另一个类时丢失数据
【发布时间】:2015-01-12 11:13:43
【问题描述】:

我做了一个简单的测试应用。我从 UIViewController 传递了一个 int 值:

namespace Test
{
    public partial class TestViewController : UIViewController
    {
        Getter gt;
        public TestViewController (IntPtr handle) : base (handle)
        {
        }

        #region View lifecycle
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            ClickBT.TouchUpInside += (object sender, EventArgs e) => {
                gt = new Getter(2);
            };

            // Perform any additional setup after loading the view, typically from a nib.
        }
}

到另一个班级:

namespace Test
{
    public class Getter
    {

        public Getter (int x)
        {
            if (x != 0)
                Console.WriteLine ("Value isn't null: ", x);
            else
                Console.WriteLine ("Value is null: ", x);
        }
    }
}

当我点击按钮时,结果总是:

2015-01-12 09:02:31.063 Teste[5184:107203] 值不为空:

2015-01-12 09:02:32.453 Teste[5184:107203] 值不为空:

2015-01-12 09:02:32.630 Teste[5184:107203] 值不为空:

但我没有价值。值应该在哪里,为空。

我在网上找不到...

【问题讨论】:

  • 您没有正确使用 Console.WriteLine 的字符串格式重载。阅读 C# 中的字符串格式化,它会对您有很大帮助。
  • 不,我试过了。结果是一样的。
  • 我已经尝试过,现在,使用标签上的值。标签正确显示值。我认为这是一个日志问题。

标签: c# ios uiviewcontroller xamarin parameter-passing


【解决方案1】:

控制台中的响应是正确的,但为了也显示数字,您需要更改您的 Console.WriteLine。

你的例子

Console.WriteLine ("Value isn't null: ", x);

应该是什么

Console.WriteLine ("Value isn't null: " + x.ToString());

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多