在一个按钮操作中推送两个不同的视图控制器-iOS

Pushing two Different view controllers in one button action-iOS

我的 iOS 应用程序有四个显示不同信息的选项卡。

在我的第二个选项卡中 viewController 我有一个按钮,在那个 button1 操作中将其命名为 button1 我已导航到 SignInViewController 屏幕,在我的第三个选项卡中,视图控制器是 loginViewController。

在这两个 ViewController 中,我都可以选择注册,而且已经存在的用户可以登录 ViewControllers.So,在 SignInViewController 中,我有一个名为 registerButton 的按钮。现在,在这个 registerButton 操作中,我已经推送了 RegisterViewController 并且与 loginViewController 中的 SignInViewController 相同,我也将按钮命名为 registerButton2。现在在这个 registerButton2 动作中,我已经推送了相同的 RegisterViewController。

现在我真正想要的是在 RegisterViewController 中,如果我从 SignInViewController 到 RegisterViewController 然后在 SaveButtonAction 中我想推送 ShippingViewController 并且如果我从 loginViewController 中经过,我有一个按钮可以在 SaveButtonAction 中将其命名为 SaveButton到 RegisterViewController 然后在 SaveButtonAction 中我想推送 `AccountViewController.

简而言之 (1)tabbaritem2-->SignInViewController-->RegisterViewController-->ShippingViewController (2)tabbaritem3-->loginViewControoler-->RegisterViewController-->AccountViewController

我已尝试在 SaveButtonAction 中使用以下代码,但它不起作用。

这是我的代码:

- (IBAction)SaveButtonAction:(id)sender{


    if(_tabBarController.tabBarItem.tag == 2){

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ShippingViewController  *shipVc = [story instantiateViewControllerWithIdentifier:@"ShippingViewController"];
        [self.navigationController pushViewController:shipVc animated:YES];

     else if(_tabBarController.tabBarItem.tag == 3){

         UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         AccountViewController *acntVC = [story instantiateViewControllerWithIdentifier:@"AccountViewController"];
         [self.navigationController pushViewController:acntVC animated:YES];

     }

}

敬请help.Thanks提前。

只需在 RegisterViewController 中使用 boolean(比方说 shouldFromLogin),然后从 LoginVC 推送时将 boolean 设置为 true,在其他情况下将其作为 false 传递。然后检查按钮操作中的 boolean 并相应地导航到不同的 VC。

演示代码:

//This code when you push to RegisterVC from LoginVC. Similar thing for other case with shouldFromLogin as NO.

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
RegisterViewController  *registerVc = [story instantiateViewControllerWithIdentifier:@"RegisterViewController"];
registerVc.shouldFromLogin=YES;
[self.navigationController pushViewController:shipVc animated:YES];

然后检查

- (IBAction)SaveButtonAction:(id)sender{
if(shouldFromLogin){
   //Pass to AccountViewController
}
else{
  //Pass to ShippingViewController
}
}

在您的 RegisterViewController.h 文件中,声明一个枚举如下:

typedef NS_ENUM(NSUInteger, RegisterViewControllerAction) {
    RegisterViewControllerActionShipping,
    RegisterViewControllerActionAccount,
}

此外,为 RegisterViewController 声明一个新的构造函数 class:

@interface RegisterViewController

@property (readonly) RegisterViewControllerAction action;
- (id)initWithAction:(RegisterViewControllerAction)action;

@end

@implementation RegisterViewController
@synthesize action = _action;

- (id)initWithAction:(RegisterViewControllerAction)action {
    if (self = [super initWithNib:xxx]) {
         _action = action;
    }
}

- (IBAction)SaveButtonAction:(id)sender {
    if (_action == RegisterViewControllerActionShipping) {
        ....
    } else if (_action == RegisterViewControllerActionAccount) {
        ....
    }
}

您可以使用选定的标签栏索引执行此操作。

if(theTabBar.selectedItem == 2){
    ShippingViewController  *shipVc = [story instantiateViewControllerWithIdentifier:@"ShippingViewController"];
    [self.navigationController pushViewController:shipVc animated:YES];
}

else if(theTabBar.selectedItem == 3){
     AccountViewController *acntVC = [story instantiateViewControllerWithIdentifier:@"AccountViewController"];
     [self.navigationController pushViewController:acntVC animated:YES];
}

//从LoginVC推送到RegisterVC时的这段代码。其他情况类似,但分配 @"signin" 而不是 @"login".

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
RegisterViewController  *registerVc = [story instantiateViewControllerWithIdentifier:@"RegisterViewController"];
registerVc.imFromLogin = @"login";
[self.navigationController pushViewController:registerVc animated:YES];
- (IBAction)SaveButtonAction:(id)sender{
    if([_imFromLogin isEqualToString:@"login"])
    {
         [self performSegueWithIdentifier:@"regToAccount" sender:nil];
    }else if ([_imFromLogin isEqualToString:@"signin"]) {
        [self performSegueWithIdentifier:@"registerToShipping" sender:nil];
    }
}