处理UIToolbar中的多个按钮(Handling multiple buttons in UIToolbar)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
处理UIToolbar中的多个按钮(Handling multiple buttons in UIToolbar)

我在UINavigationController一侧的UIToolbar使用多个UIBarButtonItem遇到问题。 我已经使用了很多关于如何在导航栏中放置多个按钮的示例中的一个(使用工具栏),当我现在正确显示它时,我总是会在按下按钮时抛出一个未知的Objective-C异常。

这是我用来添加多个按钮的完整测试UIViewController:

using System; using System.Drawing; using MonoTouch.UIKit; namespace TestProject { public class Test : UIViewController { public override void ViewDidLoad () { base.ViewDidLoad(); UIBarButtonItem btn1 = new UIBarButtonItem( "foo", UIBarButtonItemStyle.Bordered, delegate(object sender, EventArgs e) { Console.WriteLine( "asdf" ); } ); UIBarButtonItem btn2 = new UIBarButtonItem(); btn2.Style = UIBarButtonItemStyle.Bordered; btn2.Title = "bar"; btn2.Clicked += delegate(object sender, EventArgs e) { Console.WriteLine( "fdsa" ); }; UIToolbar tb = new UIToolbar( new RectangleF( 0, 0, 150, 44.0f ) ); tb.SetItems( new UIBarButtonItem[] { btn1, btn2 }, false ); NavigationItem.RightBarButtonItem = new UIBarButtonItem( tb ); } } }

我将该控制器放在UINavigationController并将其视图显示为唯一的窗口内容。 一切正常,直到我点击其中一个按钮:

Error connecting stdout and stderr (127.0.0.1:10001) 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) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (string[]) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff> Native stacktrace: 0 TestProject 0x000d0de8 mono_handle_native_sigsegv + 343 1 TestProject 0x0000f74c mono_sigsegv_signal_handler + 322 2 libSystem.B.dylib 0x906fc45b _sigtramp + 43 3 ??? 0xffffffff 0x0 + 4294967295 4 UIKit 0x01ee6cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156 5 UIKit 0x01cd44fd -[UIApplication sendAction:to:from:forEvent:] + 119 6 UIKit 0x01d64799 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x01d66c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 8 UIKit 0x01d657d8 -[UIControl touchesEnded:withEvent:] + 458 9 UIKit 0x01cf8ded -[UIWindow _sendTouchesForEvent:] + 567 10 UIKit 0x01cd9c37 -[UIApplication sendEvent:] + 447 11 UIKit 0x01cdef2e _UIApplicationHandleEvent + 7576 12 GraphicsServices 0x0404f992 PurpleEventCallback + 1550 13 CoreFoundation 0x00ea2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 14 CoreFoundation 0x00e02cf7 __CFRunLoopDoSource1 + 215 15 CoreFoundation 0x00dfff83 __CFRunLoopRun + 979 16 CoreFoundation 0x00dff840 CFRunLoopRunSpecific + 208 17 CoreFoundation 0x00dff761 CFRunLoopRunInMode + 97 18 GraphicsServices 0x0404e1c4 GSEventRunModal + 217 19 GraphicsServices 0x0404e289 GSEventRun + 115 20 UIKit 0x01ce2c93 UIApplicationMain + 1160 21 ??? 0x077efe64 0x0 + 125763172 22 ??? 0x077ef1e2 0x0 + 125759970 23 ??? 0x077ef0be 0x0 + 125759678 24 ??? 0x077ef145 0x0 + 125759813 25 TestProject 0x0000f507 mono_jit_runtime_invoke + 1332 26 TestProject 0x001ed259 mono_runtime_invoke + 137 27 TestProject 0x001ef940 mono_runtime_exec_main + 669 28 TestProject 0x001eed2a mono_runtime_run_main + 843 29 TestProject 0x000a3093 mono_jit_exec + 200 30 TestProject 0x002a163d main + 4060 31 TestProject 0x00002819 _start + 208 32 TestProject 0x00002748 start + 40 Debug info from gdb: dyld: could not load inserted library: /Users/Apple/Library/Application Support/iPhone Simulator/4.3/Applications/86C48AC1-821F-49DD-B344-58471F02D31B/TestProject.app/monotouch-fixes.dylib ================================================================= 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. =================================================================

有谁知道造成这种情况的原因,更重要的是如何避免它? 当只设置一个按钮作为RightBarButtonItem它可以正常工作。

编辑

以上错误似乎随机循环:

Error connecting stdout and stderr (127.0.0.1:10001) 2011-08-25 13:30:05.409 TestProject[1235:a0b] -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 [ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16

I have a problem using multiple UIBarButtonItems within a UIToolbar on one side of a UINavigationController. I‘ve used one of the many examples on how to put multiple buttons in the navigation bar (using a toolbar), and while I got it displaying correctly now, I always get an unkown Objective-C exception thrown when a button is pressed.

This is the full test UIViewController I use for adding multiple buttons:

using System; using System.Drawing; using MonoTouch.UIKit; namespace TestProject { public class Test : UIViewController { public override void ViewDidLoad () { base.ViewDidLoad(); UIBarButtonItem btn1 = new UIBarButtonItem( "foo", UIBarButtonItemStyle.Bordered, delegate(object sender, EventArgs e) { Console.WriteLine( "asdf" ); } ); UIBarButtonItem btn2 = new UIBarButtonItem(); btn2.Style = UIBarButtonItemStyle.Bordered; btn2.Title = "bar"; btn2.Clicked += delegate(object sender, EventArgs e) { Console.WriteLine( "fdsa" ); }; UIToolbar tb = new UIToolbar( new RectangleF( 0, 0, 150, 44.0f ) ); tb.SetItems( new UIBarButtonItem[] { btn1, btn2 }, false ); NavigationItem.RightBarButtonItem = new UIBarButtonItem( tb ); } } }

I put that controller within a UINavigationController and display its view as the only window content. Everything works fine, until I click one of the buttons:

Error connecting stdout and stderr (127.0.0.1:10001) 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) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (string[]) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff> Native stacktrace: 0 TestProject 0x000d0de8 mono_handle_native_sigsegv + 343 1 TestProject 0x0000f74c mono_sigsegv_signal_handler + 322 2 libSystem.B.dylib 0x906fc45b _sigtramp + 43 3 ??? 0xffffffff 0x0 + 4294967295 4 UIKit 0x01ee6cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156 5 UIKit 0x01cd44fd -[UIApplication sendAction:to:from:forEvent:] + 119 6 UIKit 0x01d64799 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x01d66c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 8 UIKit 0x01d657d8 -[UIControl touchesEnded:withEvent:] + 458 9 UIKit 0x01cf8ded -[UIWindow _sendTouchesForEvent:] + 567 10 UIKit 0x01cd9c37 -[UIApplication sendEvent:] + 447 11 UIKit 0x01cdef2e _UIApplicationHandleEvent + 7576 12 GraphicsServices 0x0404f992 PurpleEventCallback + 1550 13 CoreFoundation 0x00ea2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 14 CoreFoundation 0x00e02cf7 __CFRunLoopDoSource1 + 215 15 CoreFoundation 0x00dfff83 __CFRunLoopRun + 979 16 CoreFoundation 0x00dff840 CFRunLoopRunSpecific + 208 17 CoreFoundation 0x00dff761 CFRunLoopRunInMode + 97 18 GraphicsServices 0x0404e1c4 GSEventRunModal + 217 19 GraphicsServices 0x0404e289 GSEventRun + 115 20 UIKit 0x01ce2c93 UIApplicationMain + 1160 21 ??? 0x077efe64 0x0 + 125763172 22 ??? 0x077ef1e2 0x0 + 125759970 23 ??? 0x077ef0be 0x0 + 125759678 24 ??? 0x077ef145 0x0 + 125759813 25 TestProject 0x0000f507 mono_jit_runtime_invoke + 1332 26 TestProject 0x001ed259 mono_runtime_invoke + 137 27 TestProject 0x001ef940 mono_runtime_exec_main + 669 28 TestProject 0x001eed2a mono_runtime_run_main + 843 29 TestProject 0x000a3093 mono_jit_exec + 200 30 TestProject 0x002a163d main + 4060 31 TestProject 0x00002819 _start + 208 32 TestProject 0x00002748 start + 40 Debug info from gdb: dyld: could not load inserted library: /Users/Apple/Library/Application Support/iPhone Simulator/4.3/Applications/86C48AC1-821F-49DD-B344-58471F02D31B/TestProject.app/monotouch-fixes.dylib ================================================================= 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. =================================================================

Does anyone know what‘s causing this, and more importantly how to avoid it? When setting just a single button as the RightBarButtonItem it works without any problems.

edit

Above error seems to randomly cycle with this one:

Error connecting stdout and stderr (127.0.0.1:10001) 2011-08-25 13:30:05.409 TestProject[1235:a0b] -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16 [ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[__NSCFSet InvokeAction:]: unrecognized selector sent to instance 0x7168980 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/monotouch/UIKit/UIApplication.cs:26 at TestProject.Application.Main (System.String[] args) [0x00000] in /Users/Apple/Projects/TestProject/TestProject/Main.cs:16

最满意答案

将按钮声明为类变量。 GC会收集它们,以便您的事件处理程序导致内存泄漏。

Declare the buttons as class variables. The GC collects them so your event handlers are causing memory leaks.

更多推荐

本文发布于:2023-04-12 20:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/213e5c66ffd2868a332b8efdb0d9ec1a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   按钮   UIToolbar   buttons   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!