从UIView控制器传递到另一个类时丢失数据

本文关键字:数据 另一个 UIView 控制器 | 更新日期: 2023-09-27 18:37:21

我做了一个简单的测试应用程序。我从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] 值不为空:

但我没有价值。其中应该是值,为空。

我在互联网上找不到它...

从UIView控制器传递到另一个类时丢失数据

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

您的示例

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

它应该是什么

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