TamoDaleko
TamoDaleko

Reputation: 151

how to check what previous viewcontroller was open

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 ?

Upvotes: 1

Views: 1283

Answers (4)

Rohit Pradhan
Rohit Pradhan

Reputation: 3875

  • 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
    }
}

Upvotes: 2

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

@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

Upvotes: 0

Bhoomi Jagani
Bhoomi Jagani

Reputation: 2423

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(){
}

Upvotes: 0

user3432164
user3432164

Reputation:

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.
}

}

Upvotes: 2

Related Questions