MonoTouch 中的按钮问题

发布于 2024-12-21 19:26:05 字数 683 浏览 4 评论 0原文

我正在使用 MonoTouch,我提出了三个有关按钮的问题,希望有人可以帮助我。

  1. 我正在尝试更改标题栏中的“后退”按钮。我该怎么做?我已经看到这里发布的帖子:如何更改背面的文本按钮。然而,这是行不通的。我感觉我没有访问控制器属性。目前,当我尝试在 ViewDidLoad 方法中设置按钮文本时,我收到 NullReferenceException。目前,我正在尝试像这样设置文本:

    this.NavigationItem.BackBarButtonItem.Title = "返回";

    我怀疑我没有访问根控制器,但我不确定如何执行此操作。

  2. 对于单独页面上的“后退”按钮,我想执行自定义操作。我想让它像以前一样回去。但在此之前,我想执行一些自定义代码。我该如何执行此操作?

  3. 我需要在文本段落中创建看起来像超链接的内容。我该怎么做?

MonoTouch 看起来很酷。然而,学习曲线比我预期的要陡一些。谢谢你!

I'm working with MonoTouch and I have come up with three questions around buttons that I'm hoping someone can help me with.

  1. I'm trying to change the "Back" button in the title bar. How do I do this? I've seen the thread posted here: How to change text on a back button. However, that doesn't work. I get the vibe that I'm not accessing the controller property. Currently, I receive a NullReferenceException when I attempt to set the button text in the ViewDidLoad method. Currently, I'm trying to set the text like such:

    this.NavigationItem.BackBarButtonItem.Title = "Back";

    I have the suspicion that I'm not accessing the root controller, but I'm not sure how to do this.

  2. For a "back" button on a separate page, I want to perform a custom action. I want it to go back like it does. But before that happens, I want to execute some custom code. How do I do this?

  3. I need to create something that looks like a hyperlink within a paragraph of text. How do I do that?

MonoTouch seems cool. However, the learning curve is a bit steeper than I had anticipated. Thank you!

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

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

发布评论

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

评论(3

沫离伤花 2024-12-28 19:26:05

对于您的第一个问题,MonoTouch 特定的答案(针对您提供的问题)效果很好。

    this.NavigationItem.BackBarButtonItem = new UIBarButtonItem ("MyBack", UIBarButtonItemStyle.Plain, null);

确保回答所有。该代码需要在推送控制器中调用,而不是在推送控制器中调用。

更新

对于第二个问题,您可能注意到一些需要选择器委托的重载。但是它们不起作用用于后退按钮。

解决此 iOS 限制的一种方法是重写 ViewDidAppearViewDidDisappear 以获取类似的通知。

For your first question the MonoTouch-specific answer (to the question you provided) works perfectly.

    this.NavigationItem.BackBarButtonItem = new UIBarButtonItem ("MyBack", UIBarButtonItemStyle.Plain, null);

Make sure to the all the answers. This code needs to be called in the pushing controller, not the controller being pushed.

UPDATE

For your second question you likely noticed a few overloads that takes a Selector or a delegate. However they won't work for a Back button.

One way to work around this iOS limitation is to override ViewDidAppear or ViewDidDisappear to get similar notification.

剧终人散尽 2024-12-28 19:26:05

我通过在视图生成器中创建自定义按钮解决了您的第三个问题。将类型设置为自定义会删除标准边框并使其看起来像一行文本。只需更改其大小和样式即可使其看起来更像超链接。我在文本中留下了一个空白区域,并将自定义按钮放在该空白区域中。

I solved your third question by creating a custom button in de viewbuilder. Setting the type to custom removes de standard borders and makes it look like a line of text. Simply change its size and style it to make it look more like an hyperlink. I left an empty space in my text and and put my custom button in that space.

此刻的回忆 2024-12-28 19:26:05

我创建了一个扩展方法,允许您更改后退按钮文本,并可以选择使用您自己的代码处理 TouchUp 操作。它还保持与默认后退按钮相同的边框外观。

public static class MyExtensions
{
    public static void SetCustomBackButton(this UIViewController uiViewController, string buttonText, Action onClick)
    {
        uiViewController.NavigationItem.HidesBackButton = true;
        var dummyButton = UIButton.FromType (UIButtonType.Custom);
        var backButton = (UIButton)MonoTouch.ObjCRuntime.Runtime.GetNSObject (
            MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_int (
            dummyButton.ClassHandle, MonoTouch.ObjCRuntime.Selector.GetHandle ("buttonWithType:"), 101));

        backButton.SetTitle (buttonText, UIControlState.Normal);

        backButton.TouchUpInside += delegate {
            if (onClick != null)
                onClick ();
            else
                uiViewController.NavigationController.PopViewControllerAnimated (true);
        };

        uiViewController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem (backButton);
    }
}

I created an extension method that allows you to change the back button text and optionally handle the TouchUp action with your own code. It also keeps the same border look as the default back button.

public static class MyExtensions
{
    public static void SetCustomBackButton(this UIViewController uiViewController, string buttonText, Action onClick)
    {
        uiViewController.NavigationItem.HidesBackButton = true;
        var dummyButton = UIButton.FromType (UIButtonType.Custom);
        var backButton = (UIButton)MonoTouch.ObjCRuntime.Runtime.GetNSObject (
            MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_int (
            dummyButton.ClassHandle, MonoTouch.ObjCRuntime.Selector.GetHandle ("buttonWithType:"), 101));

        backButton.SetTitle (buttonText, UIControlState.Normal);

        backButton.TouchUpInside += delegate {
            if (onClick != null)
                onClick ();
            else
                uiViewController.NavigationController.PopViewControllerAnimated (true);
        };

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