Search code examples
xamarinuinavigationcontrolleruinavigationbarback-buttonxamarin-studio

How to override the functionality of Back button when using Navigation Controller in Xamarin.ios?


I'm working on Xamarin.iOS.When i move from one view controller to another a navigation bar is added to the view on which i just moved and a back button appears. On clicking the back button it returns me to the parent view. But i want some different functionality rather than returning to the parent view. Can anyone help me out!


Solution

  • This can be accomplished by creating a custom button, and then setting that button as the back button for the view controller, such as:

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
    
            UIBarButtonItem backButton = new UIBarButtonItem("title", UIBarButtonItemStyle.Bordered, handleBack);
    
            this.NavigationItem.LeftBarButtonItem = backButton;
        }
    
    
        public void handleBack(object sender, EventArgs e)
        {
            Console.WriteLine("back!");
        }
    

    I hope this helps!