我在UIBarButtonItem
一侧的UIToolbar
内使用多个UINavigationController
时遇到问题。我已经使用了很多关于如何在导航栏中放置多个按钮的示例中的一个(使用工具栏),当我现在正确显示它时,我总是会在按下按钮时抛出一个未知的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
答案 0 :(得分:6)
将按钮声明为类变量。 GC会收集它们,以便您的事件处理程序导致内存泄漏。