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

时间:2016-09-30 05:36:13

标签: ios objective-c iphone uitabbarcontroller uitabbaritem

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

在我的第二个标签viewController中,我有一个按钮,可以将它命名为button1,我已经导航到SignInViewController屏幕,在我的第三个标签视图中,控制器是loginViewController。

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

这里现在我想要的是RegisterViewController我有一个按钮让它在SaveButtonAction中将其命名为SaveButton如果我从SignInViewController到RegisterViewController然后在SaveButtonAction中我想要推送ShippingViewController我是否从loginViewController到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];

     }

}
亲切的帮助。谢谢。

4 个答案:

答案 0 :(得分:0)

只需在boolean中使用RegisterViewController(简单地说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
}
}

答案 1 :(得分:0)

在RegisterViewController.h文件中,声明如下枚举:

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

另外,为RegisterViewController类声明一个新的构造函数:

@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) {
        ....
    }
}

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

//从LoginVC推送到RegisterVC时的代码。对于其他情况类似的事情,但分配@"登录"而不是@"登录"。

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