Button Questions in MonoTouch

丶灬走出姿态 提交于 2019-12-25 02:13:57

问题


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!


回答1:


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.




回答2:


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.




回答3:


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);
    }
}


来源:https://stackoverflow.com/questions/8552553/button-questions-in-monotouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!