-1

My iOS application has four tabs that displays different information.

In my second tab viewController I have one button lets name it as button1 in that button1 action i have navigated to SignInViewController screen and in my third tab view controller is loginViewController.

In both the ViewControllers I have option to register and as well as already existed user can logged in both the ViewControllers.So here in SignInViewController I have a button named it as registerButton. Now in this registerButton action I have pushed RegisterViewController and same as SignInViewController in loginViewController also i have button named it as registerButton2. Now in this registerButton2 Action i have pushed same RegisterViewController.

Here now what i want exactly is in RegisterViewController I have a button lets name it as SaveButton in SaveButtonAction if I am gone through from SignInViewController to RegisterViewController then in SaveButtonAction i want to push ShippingViewController and if I am gone through from loginViewController to RegisterViewController then in SaveButtonAction i want to push `AccountViewController.

in short (1)tabbaritem2-->SignInViewController-->RegisterViewController-->ShippingViewController (2)tabbaritem3-->loginViewControoler-->RegisterViewController-->AccountViewController

I have tried following code in SaveButtonAction but its does not working.

Here is my code:

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

     }

}

kindly help.Thanks in Advance.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
hani
  • 21
  • 6
  • I can't really make sense of your description, but what I get is that you want to perform a different segue from one button based on some condition. In this case, create the segues from your view controller object to your destination scenes and give them identifiers. Then in your `saveButtonActin` method you can use `self.performSegueWithIdentifier`, providing whichever segue identifier is appropriate – Paulw11 Sep 30 '16 at 05:41
  • what do you want i cant understand even i have read your question 3 to 4 times but i am not able to understand.... – Pooja Srivastava Sep 30 '16 at 05:45
  • @hani i just editted your ques, approve it and dont mess with ques self. – vaibhav Sep 30 '16 at 05:59

4 Answers4

0

Just use a boolean in the RegisterViewController (lets say shouldFromLogin) and while pushing from LoginVC set the boolean as true and in other case pass it as false. Then check that boolean in the button action and navigate to different VC accordingly.

Demo Code :

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

Then check

- (IBAction)SaveButtonAction:(id)sender{
if(shouldFromLogin){
   //Pass to AccountViewController
}
else{
  //Pass to ShippingViewController
}
}
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

In your RegisterViewController.h file, declare an enum as below:

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

Also, declare a new constructor for 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) {
        ....
    }
}
Hao Xi
  • 351
  • 4
  • 12
  • thanku kevin im a new developer. in ur code initwithnib u gave xxx...but what should i write in my project – hani Sep 30 '16 at 07:02
  • I think you will only need to use `[super init]`. Give a try! – Hao Xi Sep 30 '16 at 07:26
  • yea i have tried ur code @kevin but its in two tabbar items it pushing to shippingviewcontroller only :( – hani Oct 01 '16 at 04:44
0

you can do this using selected tabbar index.

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];
}
vaibhav
  • 4,038
  • 1
  • 21
  • 51
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

//This code when you push to RegisterVC from LoginVC. Similar thing for other case but assign @"signin" instead of @"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];
    }
}
Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31
hani
  • 21
  • 6