Objective check which view the user has just come from

72 views Asked by At

I have an Application where you can go to one view from two controllers and I was wondering whether it was possible to check which it came from so that I can do different things depending on the controller it came from.

Thanks in advance

3

There are 3 answers

1
random On BEST ANSWER

You can access the UINavigation stack to see which view is before the previous one assuming you push the new view.

Class aClass = [[[self.navigationController viewControllers] objectAtIndex:self.navigationController.viewControllers.count - 2] class];

if (aClass == [UIViewControllerA class])
    //Do something
else if (aClass == [UIVIewControllerB class])
    //Do something else

Or create a custom init method for the single view you push to that allows you to pass in a variable as to which view it came from (sorry, really wordy).

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil isFromViewA:(bool)isFromViewA
6
Dipu Rajak On

I might have to write a custom initialization method and pass something along it during init.

There will be different ways to do. This is one of the way

in yourView.h

-(id)initWithType:(int)viewControllerType;

Also create a int variable suppose int viewType; in yourView.h

in yourView.m file

        -(id)initWithType:(int)viewControllerType{
        self = [super initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
        if (self) {
            //custom init here
            viewType = viewControllerType;

    }

you can define first viewController to be 1 and second to be 2.

so when initialization that yourView in first viewController. Code should be something like

yourView *newView = [yourView alloc] initWithType:1];

so when initialization that yourView in second viewController. Code should be something like

yourView *newView = [yourView alloc] initWithType:2];

Now, things will be easier

if(viewType==1){
//do something particular for first view controller 
}
if(viewType==2){
//do something particular for second view controller 
}

If it did not work. Please share you code... Thanks

0
Hive7 On

I worked it out myself. I made an nsobject and then when I left the controller I add that to the array of if it exists update it then in the next controller if the value of the element in the object is equal to the name of the controller that it came from then it does extra things