iosobjective-cenumstyped

`enum` type always getting `0` while passing it as parameter from one viewController to another


This is my enum :

typedef enum {
    ViewControllerNameSignUpViewController,
    ViewControllerNameForgotPasswordViewController,
    ViewControllerNameProfileViewController,
} ViewControllerName;

I want to send ViewControllerName from my LeftMenuViewController to HomeViewController

Code of LeftMenuViewController :

HomeViewController *controller = [HomeViewController initViewControllerWithFurtherNavigationFor:ViewControllerNameProfileViewController];
UINavigationController *navController = (UINavigationController *)self.revealViewController.frontViewController;
[navController setViewControllers:@[controller] animated:NO];
[self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];

Code of HomeViewController :

@property (assign, nonatomic) ViewControllerName viewControllerName;

.

+ (instancetype)initViewControllerWithFurtherNavigationFor:(ViewControllerName)viewControllerName
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HomeViewController *controller = [storyboard instantiateViewControllerWithIdentifier:kHomeViewControllerID];

    if (controller) {
        controller.viewControllerName = viewControllerName;
    }

    return controller;
}

Here controller.viewControllerName is getting the parameter perfectly, which is 2. But when I check it from viewDidLoad, it is always getting 0 :

- (void)viewDidLoad {
    [super viewDidLoad];
    [self leftMenuButtonInitialization];
    [self initialViewSetUp];
    [self furtherNavigation];
}

.

- (void)furtherNavigation
{
    if (self.viewControllerName == ViewControllerNameProfileViewController) {
        ProfileViewController *controller = [ProfileViewController initViewController];
        [controller presentViewController:controller animated:YES completion:nil];
    }
}

Here, self.viewControllerName always 0. When I set the value in my initViewController, Isn't it that I can access it as self.viewControllerName though out the class? Or I am missing something?
Thanks a lot in advance.


Solution

  • After digging through 1 hour, I have found the alternative. The Problem may be relay on the calling method of initViewControllerWithFurtherNavigationFor.

    I have called that method in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender with ([[segue identifier] isEqualToString:@"goToProfileViewController"]) using segue. So I don't have to instantiateViewControllerWithIdentifier again. I can simply introduce it as destinationViewController. And then can pass any associated parameter with it. So instead of using this code :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"goToProfileViewController"]) {
        HomeViewController *controller = [HomeViewController initViewControllerWithFurtherNavigationFor:ViewControllerNameProfileViewController];
        UINavigationController *navController = (UINavigationController *)self.revealViewController.frontViewController;
        [navController setViewControllers:@[controller] animated:NO];
        [self.revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES];
        }
    }
    

    Use this :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"goToProfileViewController"]) {
        HomeViewController *controller = [segue destinationViewController];
        controller.viewControllerName = ViewControllerNameProfileViewController;
        }
    }
    

    Hope it will help for this kind of problem. :)