-2

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

Hive7
  • 3,599
  • 2
  • 22
  • 38

3 Answers3

1

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
random
  • 8,568
  • 12
  • 50
  • 85
0

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

Dipu Rajak
  • 693
  • 5
  • 15
  • What do I put in this line `self = [super initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];` as the floats – Hive7 Aug 22 '13 at 18:22
  • also where do I set these lines `yourView *newView = [yourView alloc] initWithType:2];` – Hive7 Aug 22 '13 at 18:24
  • Question 1: you declare the frame of the view... for example [super initWithFrame:(0, 0, 200, 200)]; ... any frame as required by your app – Dipu Rajak Aug 22 '13 at 18:27
  • Question 2: yourView *newView = [yourView alloc] initWithType:2]; .....this is for initializing youView in secondViewController... So when you init youView in firstViewController you pass 1 and when you init youView in secondViewController you pass 2. – Dipu Rajak Aug 22 '13 at 18:33
  • Thats.. cool... but I am pretty sure logic is correct ... I have done in the past... – Dipu Rajak Aug 22 '13 at 18:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36049/discussion-between-user2525359-and-ollie-cole) – Dipu Rajak Aug 22 '13 at 19:02
-2

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

Hive7
  • 3,599
  • 2
  • 22
  • 38