C# 为什么MonoTouch会在这个简单的代码上崩溃?

C# 为什么MonoTouch会在这个简单的代码上崩溃?,c#,xamarin.ios,C#,Xamarin.ios,我完全不明白为什么MonoTouch会在这个代码上崩溃。让我概述一下达到这一点的步骤: 新项目 新建>iPhone视图,带有名为TestScreen的控制器 在Main.cs中,添加了:window.AddSubviewnew TestScreen.View; 在TestScreen UIView中添加了按钮,并在“文件的所有者”上添加了一个名为testButton的出口,它应该是我的TestScreen控制器,并将按钮链接到该出口。 然后,我添加了以下代码: public override v

我完全不明白为什么MonoTouch会在这个代码上崩溃。让我概述一下达到这一点的步骤:

新项目 新建>iPhone视图,带有名为TestScreen的控制器 在Main.cs中,添加了:window.AddSubviewnew TestScreen.View; 在TestScreen UIView中添加了按钮,并在“文件的所有者”上添加了一个名为testButton的出口,它应该是我的TestScreen控制器,并将按钮链接到该出口。 然后,我添加了以下代码:

public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            this.testButton.SetTitle("Test", UIControlState.Normal); // this works!
            this.testButton.TouchUpInside += HandleTestButtonhandleTouchUpInside;
        }

        int i = 0;
        void HandleTestButtonhandleTouchUpInside (object sender, EventArgs e)
        {
            i += 1;
            this.testButton.SetTitle("Testing " + i.ToString(), UIControlState.Normal); // this crashes!
        }
问题是:为什么this.testButton.SetTitle方法会导致崩溃

以下是崩溃时的应用程序输出:

堆栈跟踪:

0   MyApp               0x000d1965 mono_handle_native_sigsegv + 343
1   MyApp               0x0000ffb4 mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x9476845b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01c981b5 -[UIControl sendAction:to:forEvent:] + 67
5   UIKit                               0x01c9a647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6   UIKit                               0x01c991f4 -[UIControl touchesEnded:withEvent:] + 458
7   UIKit                               0x01c2e0d1 -[UIWindow _sendTouchesForEvent:] + 567
8   UIKit                               0x01c0f37a -[UIApplication sendEvent:] + 447
9   UIKit                               0x01c14732 _UIApplicationHandleEvent + 7576
10  GraphicsServices                    0x03edfa36 PurpleEventCallback + 1550
11  CoreFoundation                      0x00e21064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12  CoreFoundation                      0x00d816f7 __CFRunLoopDoSource1 + 215
13  CoreFoundation                      0x00d7e983 __CFRunLoopRun + 979
14  CoreFoundation                      0x00d7e240 CFRunLoopRunSpecific + 208
15  CoreFoundation                      0x00d7e161 CFRunLoopRunInMode + 97
16  GraphicsServices                    0x03ede268 GSEventRunModal + 217
17  GraphicsServices                    0x03ede32d GSEventRun + 115
18  UIKit                               0x01c1842e UIApplicationMain + 1160
19  ???                                 0x09a7714b 0x0 + 161968459
20  ???                                 0x09a76f1c 0x0 + 161967900
21  ???                                 0x09a7651c 0x0 + 161965340
22  ???                                 0x09a76374 0x0 + 161964916
23  ???                                 0x09a764c6 0x0 + 161965254
24  MyApp               0x0000fd6f mono_jit_runtime_invoke + 1332
25  MyApp               0x001ee239 mono_runtime_invoke + 137
26  MyApp               0x001f0920 mono_runtime_exec_main + 669
27  MyApp               0x001efd0a mono_runtime_run_main + 843
28  MyApp               0x000a3c62 mono_jit_exec + 200
29  MyApp               0x002a25eb main + 3838
30  MyApp               0x000030c9 _start + 208
31  MyApp               0x00002ff8 start + 40
至少,包装器管理为本机 MonoTouch.UIKit.UIApplication.UIApplicationMain int,字符串[],intptr,intptr at MonoTouch.UIKit.UIApplication.Main字符串[],字符串,字符串[0x00038] in/Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 在中的MonoTouch.UIKit.UIApplication.Main字符串[][0x00000]处 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31 at MyApp.Application.Main中的字符串[][0x00000] /Users/username/Projects/MyApp/MyApp/Main.cs:14 运行时调用。运行时调用对象 对象,intptr,intptr,intptr

本机堆栈跟踪:

0   MyApp               0x000d1965 mono_handle_native_sigsegv + 343
1   MyApp               0x0000ffb4 mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x9476845b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01c981b5 -[UIControl sendAction:to:forEvent:] + 67
5   UIKit                               0x01c9a647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6   UIKit                               0x01c991f4 -[UIControl touchesEnded:withEvent:] + 458
7   UIKit                               0x01c2e0d1 -[UIWindow _sendTouchesForEvent:] + 567
8   UIKit                               0x01c0f37a -[UIApplication sendEvent:] + 447
9   UIKit                               0x01c14732 _UIApplicationHandleEvent + 7576
10  GraphicsServices                    0x03edfa36 PurpleEventCallback + 1550
11  CoreFoundation                      0x00e21064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12  CoreFoundation                      0x00d816f7 __CFRunLoopDoSource1 + 215
13  CoreFoundation                      0x00d7e983 __CFRunLoopRun + 979
14  CoreFoundation                      0x00d7e240 CFRunLoopRunSpecific + 208
15  CoreFoundation                      0x00d7e161 CFRunLoopRunInMode + 97
16  GraphicsServices                    0x03ede268 GSEventRunModal + 217
17  GraphicsServices                    0x03ede32d GSEventRun + 115
18  UIKit                               0x01c1842e UIApplicationMain + 1160
19  ???                                 0x09a7714b 0x0 + 161968459
20  ???                                 0x09a76f1c 0x0 + 161967900
21  ???                                 0x09a7651c 0x0 + 161965340
22  ???                                 0x09a76374 0x0 + 161964916
23  ???                                 0x09a764c6 0x0 + 161965254
24  MyApp               0x0000fd6f mono_jit_runtime_invoke + 1332
25  MyApp               0x001ee239 mono_runtime_invoke + 137
26  MyApp               0x001f0920 mono_runtime_exec_main + 669
27  MyApp               0x001efd0a mono_runtime_run_main + 843
28  MyApp               0x000a3c62 mono_jit_exec + 200
29  MyApp               0x002a25eb main + 3838
30  MyApp               0x000030c9 _start + 208
31  MyApp               0x00002ff8 start + 40
来自gdb的调试信息:

/tmp/mono gdb命令。DGcT7h:1:源命令文件中出错:无法 自我调试

===========================================================================================执行本机代码时收到SIGSEGV。这通常表示一个致命的错误 mono运行时或用户使用的本机库之一出错 应用程序


尝试将发件人强制转换为UIButton。我希望这有帮助

void HandleTestButtonhandleTouchUpInside (object sender, EventArgs e)
    {
        i += 1;
        ((UIButton) sender).SetTitle("Testing " + i.ToString(), UIControlState.Normal);
    }

这个错误出现在我的步骤3中


我的新测试屏幕超出了范围,出于某种原因被处理掉了。将其分配给一个变量,然后将变量传递给AddSubview,这就解决了它。

在示例中,他根本没有使用发送者。问题是,当用这种方式调用testscreen时,它为什么会崩溃?默认的mainwindow.xib是testscreen吗?
this.InvokeOnMainThread(delegate{
              i += 1;
                          ((UIButton) sender).SetTitle("Testing " + i.ToString(), UIControlState.Normal);

            });