0

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!

Community
  • 1
  • 1
Mono Developer
  • 609
  • 2
  • 7
  • 22
  • Please avoid adding adding unrelated questions (e.g. back button and hyperlink) when using stackoverflow.com. They make it less likely for you to get a answer (unless someone has answers for every questions you have) which is bad for you. It also makes it less likely that your question can be useful for other people (since the question, and answers, will be difficult to find). When in doubt please refer to the FAQ. – poupou Dec 18 '11 at 15:43

3 Answers3

2

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.

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174
0

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.

Aidan
  • 118
  • 1
  • 11
0

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);
    }
}
holmes
  • 1,341
  • 9
  • 9
  • I found later that this solution does not always work. When I changed the appearance of the UINavigationBar the button returns back to a square. – holmes Apr 09 '13 at 03:08