Abe
Abe

Reputation: 1929

How to set the navigation controller to take the user to a different view in xamarin ios

My initial view has this code

if (CLLocationManager.Status == CLAuthorizationStatus.Denied ||
    CLLocationManager.Status == CLAuthorizationStatus.Restricted ||
    CLLocationManager.Status == CLAuthorizationStatus.NotDetermined)
{
    Core.FirstLaunch = true;
    NavigationController.PushViewController(new LocationServicesVerifyViewController(), false);
}
else
{
    NavigationController.PushViewController(new JobListViewController(), false);
}

If the location is not enabled, the location services verify controller is shown, however, the menu has a back button which is taking the user to license activation screen.

How can I change the back button to Job list and clicking it will take the user to job list controller instead?

e.g.,

Expected Result

Upvotes: 2

Views: 42

Answers (2)

Iain Smith
Iain Smith

Reputation: 9703

So you are pushing views on to the navigation stack, e.g.,

NavigationController.PushViewController(new LocationServicesVerifyViewController(), false);

so when you press back you are popping the stack and going to the last view. If you don't want this type of navigation you could do this:

Don't push the LocationServicesVerifyViewController to the navigation controller. Rather present it modally like this:

PresentModalViewController(new LocationServicesVerifyViewController(), false);

You will need a close button on LocationServicesVerifyViewController to dismiss.

I would present the JobListViewController by pushing to the navigation controller first then check for the location inside JobListViewController then present the modal LocationServicesVerifyViewController if needed.

OR

Do you need a whole view controller for this? You could just present a popup:

var locationServiceAlertController = UIAlertController.Create("Location Services are off", "Your company... Bla", UIAlertControllerStyle.Alert);
locationServiceAlertController.AddAction(UIAlertAction.Create("Enable", UIAlertActionStyle.Default, alert => Console.WriteLine ("Do your enable stuff")));
locationServiceAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine ("Cancel was clicked")));
PresentViewController(locationServiceAlertController, true, null);

Upvotes: 0

Saamer
Saamer

Reputation: 5099

Solution 1: You need to replace the backbutton and associate an action handler. You could either use text or an image for the Back Button. To do this, in your ViewDidLoad override, add this:

var _backButton = new UIBarButtonItem()
{
            Title = "Back",
            // Image = UIImage.FromBundle("back_navbar").OriginalRendering(), Need to add the image in Resources for this to work
            TintColor = UIColor.FromRGB(0, 70, 113) // Change this to your app colours
};
_backButton.Clicked += _backButton_Clicked;
NavigationItem.SetLeftBarButtonItem(_backButton, animated: true);

And then handle the button press as shown:

void _backButton_Clicked(object sender, System.EventArgs e)
{
         var controller = new JobListViewController();
         this.PresentViewController(controller, true, null);
}

Solution 2: You could also just remove the unwanted LicenseActivation view controller from the navigation stack so going back won't go to that page. So when you are leaving the LicenseActivation page, in the ViewWillDisappear override, add this code:

var oldControllers = this.NavigationController.ViewControllers;
var newControllers = new UIViewController[oldControllers - 1];
int index = 0;
foreach (var item in oldControllers) {
     if (item != this) 
     {
          newControllers [index] = item;
          index++;
     }
}
this.NavigationController.ViewControllers = newControllers;

Solution 3: Just use a modal for the LicenseActivation page, so that as soon as you Accept the LicenseActivation, it closes the modal and goes to the next page.

Upvotes: 1

Related Questions