CamsDumbOnes CamsDumbOnes -4 years ago 108
Objective-C Question

Obtaining ViewController Identifier for "Back" UIButton no UINavigationController

I am very new to Xcode and Objective-C as a whole. I have begun my first coding project/app this past week and I am running into a quick problem.

When creating my app, I started by dragging ViewControllers from the right Utilities menu section into my workspace area. I then added UIButtons from the same menu and Control+dragged a modal connection between the two (when the button is pressed, the view changes from one controller to the other?). My experience has been largely UI based and very little coding.

My Issue:

I have 5 ViewControllers each individually dragged from the Utilities menu. Four of them are linked by UIButtons titled "Next" and "Prev" according to the order I want them navigated. If at any time, a button present on all for ViewControllers is pressed on any given ViewController, you are presented with a separate "Menu" ViewController that has a "Back" UIButton. When this button is pressed, I want the ViewController to be switched back to whichever of the 4 VC's I was previously on when I pressed "Menu." All of the ViewControllers are in class ViewController.

Logically, this can be accomplished through setting a couple integers:

@implementation ViewController

int buttonPressedHere = 0;

- (IBAction)view1menu:(id)sender{
buttonPressedHere = 1;

- (IBAction)view2menu:(id)sender{
buttonPressedHere = 2;

- (IBAction)view3menu:(id)sender{
buttonPressedHere = 3;

- (IBAction)view4menu:(id)sender{
buttonPressedHere = 4;

- (void)setPrevious{
if(buttonPressedHere == 1)
//insert code to return to previous ViewController 1
if(buttonPressedHere == 2)
//insert code to return to previous ViewController 2
if(buttonPressedHere == 3)
//insert code to return to previous ViewController 3
if(buttonPressedHere == 4)
//insert code to return to previous ViewController 4


The issue here is, I don't know how to get the identifier of the ViewController I want to switch to. Is there an easier way? Help!

Answer Source

First, define class for previous ViewControllers in Xib or Storyboard:enter image description here

This is usually needed, you will custom the previous four views' activities further. The custom view controller class should be inherited from UIViewController.

Then, you are able to control the pop action by following code:

 - (void)setPrevious {    
        if (buttonPressedHere == 1) {
            [self popToPreviousViewControllerWithClass:[FirstViewController class]];
        else if (buttonPressedHere == 2) {
            [self popToPreviousViewControllerWithClass:[SecondViewController class]];
        else if (buttonPressedHere == 3) {
            [self popToPreviousViewControllerWithClass:[ThirdViewController class]];
        else if (buttonPressedHere == 4) {
            [self popToPreviousViewControllerWithClass:[FourthViewController class]];

    - (void)popToPreviousViewControllerWithClass:(Class)class {
        for (UIViewController *previousViewController in self.navigationController.viewControllers) {
            if ([previousViewController isKindOfClass:class]) {
                [self.navigationController popToViewController:previousViewController animated:YES];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download