0

Here is my question:

I have 3 different UIViewControllers;

The first is A, the second is B, and the third is C.

A has a push segue to B and then I'm doing some stuff in the ViewDidLoad method. C also has a segue to B and then i=I need to do other stuff in the ViewDidLoad method.

Is there any way to know which UIViewController A or C has pushed to B?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
OshriALM
  • 215
  • 3
  • 12
  • ok ill be more specific. i have a user profile view which has few UITextField. in the profile i have a button that push to search view where the user can choose which country and city he lives. after the user choosed i need to change to values in the first view (A) so i need to know which view is pushed to A because – OshriALM Jul 24 '14 at 18:32
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – duci9y Jul 24 '14 at 18:40

3 Answers3

3

You could have a property in the .h file in class B like this:

@property (nonatomic) NSString *viewControllerName;

And in the -prepareForSegue: method in class A and class C, you could set the viewControllerName property to equal class A's name or class C's name.
Then a simple if-else check in class B's viewDidLoad should help you load it accordingly.

Example B.h:

#import <UIKit/UIKit.h>

@interface B : UIViewController 

@property (nonatomic) NSString *view;

@end

Example C.m or A.m

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        B *vc = [segue destinationViewController];
        vc.view = @"C"; // Change to A if this is in A.m
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
1

Yes . In your navigation Stack you will be able to trace it as which view had pushed the present view

NSArray *arr=[self.navigationController ViewControllers];

IOf you print his array you will get a backtrace of all the viewCOntroller in the navigation Stack

Geet
  • 2,427
  • 2
  • 21
  • 39
  • not all the ViewControllers have a NavigationController – OshriALM Jul 24 '14 at 18:39
  • After what I read from your comment , I am guessing you need to pass some data from one ViewController to the next is it correct??...any ways evenif our are presenting or dismissing a controller you can figure out its parentViewControlller by using self.presentingViewController.parentVIewCOntroller – Geet Jul 24 '14 at 18:47
1

In the .m of class B, lets call it, ViewController_B.m

#import "ViewController_A.h"
#import "ViewController_C.h"

//...

-(void)viewDidLoad
{
    //...

    NSArray *arrViewControllers = self.navigationController.viewControllers;

    if (arrViewControllers.count <= 1) { //not needed but just incase, maybe?
        NSLog(@"No parent");
        return;
    }

    id vcCurrent = arrViewControllers[arrViewControllers.count-2];
    if ([vcCurrent isKindOfClass:[ViewController_A class]]) {
        NSLog(@"Pushed by A");
        //Do class A specific things

    }
    else if ([vcCurrent isKindOfClass:[ViewController_C class]]) {
        NSLog(@"Pushed by C");
        //Do class C specific things

    }
}

I have, however, never done it this way.
I would keep a parameter in class B to identify what logic to load it with. (as suggested by @WyattMufson in his answer above/below)

Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98