`enum`类型总是得到`0`,同时将它作为参数从一个viewController传递给另一个

时间:2017-06-03 23:10:41

标签: ios objective-c enums typed

这是我的enum

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

我想从ViewControllerName发送LeftMenuViewControllerHomeViewController

LeftMenuViewController代码:

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

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;
}

此处controller.viewControllerName完美地获取参数,即2。但是当我从viewDidLoad检查时,它始终是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];
    }
}

此处,self.viewControllerName始终为0。当我在initViewController中设置值时,我是否可以在课堂上以self.viewControllerName的形式访问它?或者我错过了什么? 非常感谢。

1 个答案:

答案 0 :(得分:0)

经过1小时的挖掘,我找到了替代方案。问题可能是initViewControllerWithFurtherNavigationFor的调用方法的中继。

我使用segue在- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender中使用([[segue identifier] isEqualToString:@"goToProfileViewController"])调用了该方法。所以我不必再次instantiateViewControllerWithIdentifier。我可以简单地将其引入destinationViewController。然后可以传递任何相关的参数。所以不要使用这段代码:

- (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];
    }
}

使用此:

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

希望它能帮助解决这类问题。 :)