1

I have ViewController A B and C. Form B and C I can navigate back to the ViewController A.

First shown is ViewController A after that user can either go to B or C

I want to present a message into a text box on ViewController A only if the if the user comes back from the ViewContoller C

So how can I identify/check if the user comes from that specific ViewController C ?

2
  • 1
    While jumping form one vc to another you could pass a flag/value to check at vc-A for which vc it came from. Also you could check out the stack for the one which pushed link for your reference Commented Feb 1, 2016 at 9:25
  • You can simply use a delegate for this! Commented Feb 1, 2016 at 9:25

4 Answers 4

2

Solution 1:

You could use delegation to do this.

You could define a protocol, say, RootViewControllerDelegate.

RooViewController would have a delegate that conforms to this protocol.

Your TopViewController would conform to this protocol, which could have a method such as -RootViewControllerCompletedSomeThing, which the RootViewController could send when it is finished.

In TopViewController's implementation of this method, it could dismiss/pop RootViewController, and do whatever else it is that you want to do when RootViewController has been dismissed.

Solution 2:

You ca set NSUserDefaults to every view controller.

For example For ViewController A

[[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"ViewControllerA"]; 

For example For ViewController B

[[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"ViewControllerB"]; 

For example For ViewController C

[[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"ViewControllerC"]; 

And In A Viewcontroller ViewDidLoad check belowCondition

-(Void)ViewDidLoad { if([[NSUserDefaults standardUserDefaults]ValueForKey:@"ViewControllerC"] isEqualToString:@"Yes"]) { //Write your code. } } 
Sign up to request clarification or add additional context in comments.

5 Comments

tnx allot for your answer but I have a slight problem that the if statement is already true before I even was on the ViewController C
Solution 2 will fail if Go to C .Now you have set key to YES Then back to A It will work.Now if I go to B & back to A ,It will show me Text in textView Because earlier I have set Key to Yes .One more thing is viewDidLoad will never gets called when I pop ViewController
One More thing to add you are making NSUserDefaults heavy .Which is not good practice beacuse it will stay in whole APP lifeCyle
@RohitKP Tnx for the tip !
@TamoDaleko:Which logic you are using now.
2
  • You can take one property in A viewController , just before you push B Or C ViewController into the navigationController you can set this property with the nane of the viewController you are pushing.
  • When you pop ViewController C or B,viewWillAppear of A gets called where you can set the textField with the name of controller if it is C you have set earlier while pushing.

//while Pushing C

ViewController *cViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"cViewControllerID"]; self.viewControllerPushed = @"cVC"; [self.navigationController pushViewController:cViewController animated:YES]; 

//while Pushing B

ViewController *bViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"bViewControllerID"]; self.viewControllerPushed = @"bVC"; [self.navigationController pushViewController:bViewController animated:YES]; 

//in viewWillAppear

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if([self.viewControllerPushed isEqualToString:@"cVC"]){ //show in textField } } 

2 Comments

do you have a example for that ?
@TamoDaleko, the answer given that is not less-then the example. Just go through the step suggested in answer and at least just try.
0

You can simple use delegate or notification pattern... For ex Notification Pattern.

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeTextFieldData", name:kNotificationFromBackFromVcC , object: nil) func changeTextFieldData(){ //set data as you want in your textfield... } 

in your method...

and at the btnClick event in ViewController C you can call...

`NSNotificationCenter.defaultCenter().postNotificationName(name:kNotificationFromBackFromVcC, object: nil`) 

For ex. Delegate Pattern

in you ViewController C

protocol BackClickDelegate { func backlickFromVC(isChange:Bool); } 

at btnClick event in ViewController C write

if self.delegate?.respondsToSelector(Selector("backClick")) { ... do your works } 

in your viewcontroller A you can implement that delegate method .

 func backClick(){ } 

1 Comment

it is tagged objective C
0
@interface viewController(){ NSString strViewController; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if(strViewController){ if([strViewController isEqualToString:@"B"]){ self.textFieldMsg.text=@"B"; } else{ self.textFieldMsg.text=@"C"; } } } //Befor Puch to B or C from ViewController A just assigne value for variable strViewController like strViewController=@"B" for pushViewController:viewControllerB strViewController=@"C" for pushViewController:viewControllerC 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.