这是我的enum
:
typedef enum {
ViewControllerNameSignUpViewController,
ViewControllerNameForgotPasswordViewController,
ViewControllerNameProfileViewController,
} ViewControllerName;
我想从ViewControllerName
发送LeftMenuViewController
到HomeViewController
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
的形式访问它?或者我错过了什么?
非常感谢。
答案 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;
}
}
希望它能帮助解决这类问题。 :)