Monotouch:以编程方式使用动态 ViewController 创建 UITabbar
我尝试了很多方法并通过网络搜索但没有成功。这是我尝试过的最简单的方法,但无法运行它:
如果我运行此代码,它会正确显示选项卡栏,但是当我单击选项卡时,我会得到一个异常,我将其粘贴在以下代码示例下。
这是我的 MainMenu.cs 中的一个方法,它是入口 xib。
我很感激对此的任何帮助。预先非常感谢您!
private void Render_Screen (object sender, RenderScreenEventArgs e)
{
UITabBarController tabbarController = new UITabBarController();
UINavigationController navController1 = new UINavigationController();
UINavigationController navController2 = new UINavigationController();
UIViewController a = new UIViewController();
a.TabBarItem = new UITabBarItem(UITabBarSystemItem.Contacts, 0);
UIViewController b = new UIViewController();
b.TabBarItem = new UITabBarItem(UITabBarSystemItem.Favorites, 1);
UIViewController[] arrayA = new UIViewController[] { a };
UIViewController[] arrayB = new UIViewController[] { b };
navController1.ViewControllers = arrayA;
navController2.ViewControllers = arrayB;
UINavigationController[] arrayC = new UINavigationController[] { navController1, navController2 };
tabbarController.ViewControllers = arrayC;
InvokeOnMainThread (delegate {
this.View.AddSubview (tabbarController.View);
});
}
Stacktrace:
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at NotifyMe.Application.Main (string[]) [0x00000] in /Users/hschulz/Projects/NotifyMe/NotifyMe/Main.cs:16
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 NotifyMe 0x000e1008 mono_handle_native_sigsegv + 408
1 NotifyMe 0x00011c7f mono_sigsegv_signal_handler + 351
2 libsystem_c.dylib 0x9753459b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
5 UIKit 0x024ce569 -[UITabBar _sendAction:withEvent:] + 466
6 CoreFoundation 0x012d8ec9 -[NSObject performSelector:withObject:withObject:] + 73
7 UIKit 0x022a95c2 -[UIApplication sendAction:to:from:forEvent:] + 96
8 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
9 UIKit 0x0234eb76 -[UIControl sendAction:to:forEvent:] + 66
10 UIKit 0x0234f03f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
11 UIKit 0x0234ebab -[UIControl sendActionsForControlEvents:] + 48
12 UIKit 0x024d0d1f -[UITabBar(Static) _buttonUp:] + 123
13 CoreFoundation 0x012d8ec9 -[NSObject performSelector:withObject:withObject:] + 73
14 UIKit 0x022a95c2 -[UIApplication sendAction:to:from:forEvent:] + 96
15 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
16 UIKit 0x0234eb76 -[UIControl sendAction:to:forEvent:] + 66
17 UIKit 0x0234f03f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
18 UIKit 0x0234e2fe -[UIControl touchesEnded:withEvent:] + 549
19 UIKit 0x022cea30 -[UIWindow _sendTouchesForEvent:] + 513
20 UIKit 0x022cec56 -[UIWindow sendEvent:] + 273
21 UIKit 0x022b5384 -[UIApplication sendEvent:] + 464
22 UIKit 0x022a8aa9 _UIApplicationHandleEvent + 8196
23 GraphicsServices 0x048a0fa9 PurpleEventCallback + 1274
24 CoreFoundation 0x012ab1c5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
25 CoreFoundation 0x01210022 __CFRunLoopDoSource1 + 146
26 CoreFoundation 0x0120e90a __CFRunLoopRun + 2218
27 CoreFoundation 0x0120ddb4 CFRunLoopRunSpecific + 212
28 CoreFoundation 0x0120dccb CFRunLoopRunInMode + 123
29 GraphicsServices 0x0489f879 GSEventRunModal + 207
30 GraphicsServices 0x0489f93e GSEventRun + 114
31 UIKit 0x022a6a9b UIApplicationMain + 1175
32 ??? 0x0e05a305 0x0 + 235250437
33 ??? 0x0e058b90 0x0 + 235244432
34 ??? 0x0e058888 0x0 + 235243656
35 ??? 0x0e0589de 0x0 + 235243998
36 NotifyMe 0x000119cf mono_jit_runtime_invoke + 1407
37 NotifyMe 0x0022014a mono_runtime_invoke + 170
38 NotifyMe 0x00222e81 mono_runtime_exec_main + 705
39 NotifyMe 0x00222091 mono_runtime_run_main + 929
40 NotifyMe 0x000ad5bf mono_jit_exec + 239
41 NotifyMe 0x002f41aa main + 5194
42 NotifyMe 0x00003035 start + 53
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
I tried so many approaches and searched through the web with no success. Here is the simplest approach I tried and can not get it to run:
If I run this code it displays the Tabbar correctly but when I click on the tabs I get an exception which I pasted under the following code sample.
This is a method from my MainMenu.cs which is the entry xib.
I appreciate any help to this. Thank you very much in advance!
private void Render_Screen (object sender, RenderScreenEventArgs e)
{
UITabBarController tabbarController = new UITabBarController();
UINavigationController navController1 = new UINavigationController();
UINavigationController navController2 = new UINavigationController();
UIViewController a = new UIViewController();
a.TabBarItem = new UITabBarItem(UITabBarSystemItem.Contacts, 0);
UIViewController b = new UIViewController();
b.TabBarItem = new UITabBarItem(UITabBarSystemItem.Favorites, 1);
UIViewController[] arrayA = new UIViewController[] { a };
UIViewController[] arrayB = new UIViewController[] { b };
navController1.ViewControllers = arrayA;
navController2.ViewControllers = arrayB;
UINavigationController[] arrayC = new UINavigationController[] { navController1, navController2 };
tabbarController.ViewControllers = arrayC;
InvokeOnMainThread (delegate {
this.View.AddSubview (tabbarController.View);
});
}
Stacktrace:
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
at NotifyMe.Application.Main (string[]) [0x00000] in /Users/hschulz/Projects/NotifyMe/NotifyMe/Main.cs:16
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
0 NotifyMe 0x000e1008 mono_handle_native_sigsegv + 408
1 NotifyMe 0x00011c7f mono_sigsegv_signal_handler + 351
2 libsystem_c.dylib 0x9753459b _sigtramp + 43
3 ??? 0xffffffff 0x0 + 4294967295
4 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
5 UIKit 0x024ce569 -[UITabBar _sendAction:withEvent:] + 466
6 CoreFoundation 0x012d8ec9 -[NSObject performSelector:withObject:withObject:] + 73
7 UIKit 0x022a95c2 -[UIApplication sendAction:to:from:forEvent:] + 96
8 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
9 UIKit 0x0234eb76 -[UIControl sendAction:to:forEvent:] + 66
10 UIKit 0x0234f03f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
11 UIKit 0x0234ebab -[UIControl sendActionsForControlEvents:] + 48
12 UIKit 0x024d0d1f -[UITabBar(Static) _buttonUp:] + 123
13 CoreFoundation 0x012d8ec9 -[NSObject performSelector:withObject:withObject:] + 73
14 UIKit 0x022a95c2 -[UIApplication sendAction:to:from:forEvent:] + 96
15 UIKit 0x022a955a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
16 UIKit 0x0234eb76 -[UIControl sendAction:to:forEvent:] + 66
17 UIKit 0x0234f03f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
18 UIKit 0x0234e2fe -[UIControl touchesEnded:withEvent:] + 549
19 UIKit 0x022cea30 -[UIWindow _sendTouchesForEvent:] + 513
20 UIKit 0x022cec56 -[UIWindow sendEvent:] + 273
21 UIKit 0x022b5384 -[UIApplication sendEvent:] + 464
22 UIKit 0x022a8aa9 _UIApplicationHandleEvent + 8196
23 GraphicsServices 0x048a0fa9 PurpleEventCallback + 1274
24 CoreFoundation 0x012ab1c5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
25 CoreFoundation 0x01210022 __CFRunLoopDoSource1 + 146
26 CoreFoundation 0x0120e90a __CFRunLoopRun + 2218
27 CoreFoundation 0x0120ddb4 CFRunLoopRunSpecific + 212
28 CoreFoundation 0x0120dccb CFRunLoopRunInMode + 123
29 GraphicsServices 0x0489f879 GSEventRunModal + 207
30 GraphicsServices 0x0489f93e GSEventRun + 114
31 UIKit 0x022a6a9b UIApplicationMain + 1175
32 ??? 0x0e05a305 0x0 + 235250437
33 ??? 0x0e058b90 0x0 + 235244432
34 ??? 0x0e058888 0x0 + 235243656
35 ??? 0x0e0589de 0x0 + 235243998
36 NotifyMe 0x000119cf mono_jit_runtime_invoke + 1407
37 NotifyMe 0x0022014a mono_runtime_invoke + 170
38 NotifyMe 0x00222e81 mono_runtime_exec_main + 705
39 NotifyMe 0x00222091 mono_runtime_run_main + 929
40 NotifyMe 0x000ad5bf mono_jit_exec + 239
41 NotifyMe 0x002f41aa main + 5194
42 NotifyMe 0x00003035 start + 53
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
导致该异常的原因是您的应用程序是使用 5.0 SDK 构建的,但您使用的是 4.3 SDK 运行。几周前我遇到了这个问题。
为了解决这个问题,当模拟器通知您崩溃时,会有一个按钮显示“Switch SDK”。选择它,然后它将显示可用的 SDK。从列表中选择 5.0。重新启动,这将解决您的问题。
The exception is caused because your application is built with the 5.0 SDK, but you are running with the 4.3 SDK. I ran into this a few weeks ago.
To fix this problem, when the Simulator informs you of the crash, there is a button that says "Switch SDK". Select that, and then it will show you the available SDKs. Pick 5.0 from the list. Restart, and that will fix your problem.
看来在外部声明工具栏可以解决我的问题。
It seems that declaring the toolbar outside fixes the issue for me.