上次更新后 NavigationItem RightBarButtonItem 出现问题

发布于 2024-10-31 23:43:58 字数 5182 浏览 1 评论 0原文

上次更新(4.0.0)后,我遇到了一个问题,想知道这是一个错误,是否有修复。

正如您从下面的代码中看到的,我正在创建一个 NavigationItem.RightBarButtonItem 并向 TouchUpInside 事件添加委托。按钮已创建,但当触摸发生时,应用程序会可怕地死掉(见下文)。在此更新之前,代码运行完美。

有人有什么想法吗? Monotouch团队,您知道这个问题吗?也许我只是编码不正确并且之前工作过,但新的更新证明我编码不正确。

问候,

丹尼

代码

        //start by creating the view
        RectangleF f = new RectangleF(0f,0f,38f,38f);
        UIView v = new UIView(f);

        //Now create a button with a background
        UIButton b = new UIButton(f);
        b.SetBackgroundImage(UIImage.FromFile("Images/Common/info.png"),UIControlState.Normal);

        //add the button to the view
        v.Add(b);
        b.TouchUpInside
        //give teh button a delegate action
        b.TouchUpInside += delegate {
            //var vc3 = new iPhone_HomeView();
            //var vc3 = new iPhone_RetardView();
            //vc3.Title = "asdasd";

            //this.NavigationController.PushViewController (vc3, true);
        };

        //now apply the view to the navigation bar on the right
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(v);
            NavigationItem.RightBarButtonItem.Width = 38f;

错误

堆栈跟踪:

在(包装器托管到本机)MonoTouch.UIKit.UIApplication.UIApplicationMain(int,string [],intptr,intptr) 在 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 中的 MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] 在 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31 中的 MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] 在 /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15 中的 SmartPhoneSolution.Application.Main (string[]) [0x00000] 在(包装器运行时调用).runtime_invoke_void_object(对象,intptr,intptr,intptr)

本机堆栈跟踪:

0   SmartPhoneSolution1                 0x000d0db5 mono_handle_native_sigsegv + 343
1   SmartPhoneSolution1                 0x0000f80c mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x996fb45b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01b92de0 -[UIControl sendAction:to:forEvent:] + 67
5   UIKit                               0x01b95262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6   UIKit                               0x01b93e0f -[UIControl touchesEnded:withEvent:] + 458
7   UIKit                               0x01b2b3d0 -[UIWindow _sendTouchesForEvent:] + 567
8   UIKit                               0x01b0ccb4 -[UIApplication sendEvent:] + 447
9   UIKit                               0x01b119bf _UIApplicationHandleEvent + 7672
10  GraphicsServices                    0x05768822 PurpleEventCallback + 1550
11  CoreFoundation                      0x00debff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12  CoreFoundation                      0x00d4c807 __CFRunLoopDoSource1 + 215
13  CoreFoundation                      0x00d49a93 __CFRunLoopRun + 979
14  CoreFoundation                      0x00d49350 CFRunLoopRunSpecific + 208
15  CoreFoundation                      0x00d49271 CFRunLoopRunInMode + 97
16  GraphicsServices                    0x0576700c GSEventRunModal + 217
17  GraphicsServices                    0x057670d1 GSEventRun + 115
18  UIKit                               0x01b15af2 UIApplicationMain + 1160
19  ???                                 0x0ba72744 0x0 + 195503940
20  ???                                 0x0ba7267a 0x0 + 195503738
21  ???                                 0x090bfb9e 0x0 + 151780254
22  ???                                 0x090bfaea 0x0 + 151780074
23  ???                                 0x090bfb75 0x0 + 151780213
24  SmartPhoneSolution1                 0x0000f5c7 mono_jit_runtime_invoke + 1332
25  SmartPhoneSolution1                 0x001ed281 mono_runtime_invoke + 137
26  SmartPhoneSolution1                 0x001ef968 mono_runtime_exec_main + 669
27  SmartPhoneSolution1                 0x001eed52 mono_runtime_run_main + 843
28  SmartPhoneSolution1                 0x000a3153 mono_jit_exec + 200
29  SmartPhoneSolution1                 0x002a16b8 main + 4155
30  SmartPhoneSolution1                 0x00002949 _start + 208
31  SmartPhoneSolution1                 0x00002878 start + 40

来自gdb的调试信息:

/tmp/mono-gdb-commands.WL0ypx:1:源命令文件中的错误: 无法调试自身

================================================ =================== 执行本机代码时收到 SIGSEGV。这通常表明 本机库之一中的致命错误

单声道运行时或应用程序使用的

。未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例 at(包装器托管到本机)MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 在 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 中的 MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.StringprincipalClassName, System.StringdelegateClassName) [0x00038] 在 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31 中的 MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] 在 /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15 中的 SmartPhoneSolution.Application.Main (System.String[] args) [0x00000]

After the last update (4.0.0) I am having an issue and was wondering of this is a bug and is there a fix.

As you can see from the code below, I am creating a NavigationItem.RightBarButtonItem and adding a delegate to the TouchUpInside event. The button is created but when the touch occurs, the app dies a horribly(see below). Before this update the code ran perfect.

Anyone has any idea? Monotouch team, are you aware of this issue? Maybe I just coded incorrectly and worked before, but the new update has proven I was coding incorrect.

Regards,

Danny

CODE

        //start by creating the view
        RectangleF f = new RectangleF(0f,0f,38f,38f);
        UIView v = new UIView(f);

        //Now create a button with a background
        UIButton b = new UIButton(f);
        b.SetBackgroundImage(UIImage.FromFile("Images/Common/info.png"),UIControlState.Normal);

        //add the button to the view
        v.Add(b);
        b.TouchUpInside
        //give teh button a delegate action
        b.TouchUpInside += delegate {
            //var vc3 = new iPhone_HomeView();
            //var vc3 = new iPhone_RetardView();
            //vc3.Title = "asdasd";

            //this.NavigationController.PushViewController (vc3, true);
        };

        //now apply the view to the navigation bar on the right
            NavigationItem.RightBarButtonItem = new UIBarButtonItem(v);
            NavigationItem.RightBarButtonItem.Width = 38f;

ERROR

Stacktrace:

at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at SmartPhoneSolution.Application.Main (string[]) [0x00000] in /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15
at (wrapper runtime-invoke) .runtime_invoke_void_object (object,intptr,intptr,intptr)

Native stacktrace:

0   SmartPhoneSolution1                 0x000d0db5 mono_handle_native_sigsegv + 343
1   SmartPhoneSolution1                 0x0000f80c mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x996fb45b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01b92de0 -[UIControl sendAction:to:forEvent:] + 67
5   UIKit                               0x01b95262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6   UIKit                               0x01b93e0f -[UIControl touchesEnded:withEvent:] + 458
7   UIKit                               0x01b2b3d0 -[UIWindow _sendTouchesForEvent:] + 567
8   UIKit                               0x01b0ccb4 -[UIApplication sendEvent:] + 447
9   UIKit                               0x01b119bf _UIApplicationHandleEvent + 7672
10  GraphicsServices                    0x05768822 PurpleEventCallback + 1550
11  CoreFoundation                      0x00debff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
12  CoreFoundation                      0x00d4c807 __CFRunLoopDoSource1 + 215
13  CoreFoundation                      0x00d49a93 __CFRunLoopRun + 979
14  CoreFoundation                      0x00d49350 CFRunLoopRunSpecific + 208
15  CoreFoundation                      0x00d49271 CFRunLoopRunInMode + 97
16  GraphicsServices                    0x0576700c GSEventRunModal + 217
17  GraphicsServices                    0x057670d1 GSEventRun + 115
18  UIKit                               0x01b15af2 UIApplicationMain + 1160
19  ???                                 0x0ba72744 0x0 + 195503940
20  ???                                 0x0ba7267a 0x0 + 195503738
21  ???                                 0x090bfb9e 0x0 + 151780254
22  ???                                 0x090bfaea 0x0 + 151780074
23  ???                                 0x090bfb75 0x0 + 151780213
24  SmartPhoneSolution1                 0x0000f5c7 mono_jit_runtime_invoke + 1332
25  SmartPhoneSolution1                 0x001ed281 mono_runtime_invoke + 137
26  SmartPhoneSolution1                 0x001ef968 mono_runtime_exec_main + 669
27  SmartPhoneSolution1                 0x001eed52 mono_runtime_run_main + 843
28  SmartPhoneSolution1                 0x000a3153 mono_jit_exec + 200
29  SmartPhoneSolution1                 0x002a16b8 main + 4155
30  SmartPhoneSolution1                 0x00002949 _start + 208
31  SmartPhoneSolution1                 0x00002878 start + 40

Debug info from gdb:

/tmp/mono-gdb-commands.WL0ypx:1: Error in sourced command file:
unable to debug self

=================================================================
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.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
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 /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at SmartPhoneSolution.Application.Main (System.String[] args) [0x00000] in /Users/aprdev/Projects/SmartPhoneSolution/SmartPhoneSolution/Main.cs:15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-11-07 23:43:58

上周我遇到了类似问题。看起来 MT 4 的 GC 比早期版本更加激进,以前我能够摆脱的事情现在却引发了问题。

尝试在类级别声明你的 UIButton,而不是在本地定义它的范围。

I had a similar problem last week. It seems like MT 4 is more aggressive in it's GC than earlier versions, and things that I was able to get away with before are now causing problems.

Try declaring your UIButton at the class level, instead of scoping it locally.

反差帅 2024-11-07 23:43:58

我在移植现有的 Objective-C 代码时也发生了同样的事情。此解决方案是确保您的按钮是您的类的实例成员。此代码运行良好(省略了额外的视图控制器样板代码):

public partial class HomeViewController : UIViewController
{
    private UIButton _settingsButton;

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

    private void ConfigureNavigationBar()
    {
        // Add the Settings icon to nav bar

        _settingsButton = UIButton.FromType(UIButtonType.Custom);

        _settingsButton.SetImage(UIImage.FromBundle("Images/settings.png"),
            UIControlState.Normal);

        _settingsButton.SizeToFit();

        _settingsButton.TouchUpInside += (sender, e) =>
            Console.WriteLine("clicked");

        this.NavigationItem.RightBarButtonItem =
            new UIBarButtonItem(_settingsButton);
    }
}

请参阅 MonoTouch 文档中的最后一段(“何时保留对对象的引用”): API 设计

Same thing happened to me porting existing Objective-C code. This solution is to make sure your button is an instance member of your class. This code works well (extra viewcontroller boilerplate code omitted):

public partial class HomeViewController : UIViewController
{
    private UIButton _settingsButton;

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

    private void ConfigureNavigationBar()
    {
        // Add the Settings icon to nav bar

        _settingsButton = UIButton.FromType(UIButtonType.Custom);

        _settingsButton.SetImage(UIImage.FromBundle("Images/settings.png"),
            UIControlState.Normal);

        _settingsButton.SizeToFit();

        _settingsButton.TouchUpInside += (sender, e) =>
            Console.WriteLine("clicked");

        this.NavigationItem.RightBarButtonItem =
            new UIBarButtonItem(_settingsButton);
    }
}

See the last paragraph ("When to Keep References to Objects") in the MonoTouch documentation: API Design

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文