doubtman
doubtman

Reputation: 61

How to check from which view controller we are coming from to another view controller

I have one screen like screen1vc and screen2vc. Now i have one view controller called commonvc.

My doubt is. in both screen1vc and screen2vc i will allow user to see the commonvc screen. But in the viewdidload or viewdidapper i need to check fom which viewcontroller user is coming.

like

if (screen1vc) 

{

}   else if (screen2vc){

}

How can i do please help me out.I am making push seague to move to commonvc

 self.navigationController?.pushViewController(commonvc, animated: true)

Thanks

Upvotes: 0

Views: 1016

Answers (5)

Deepak Carpenter
Deepak Carpenter

Reputation: 1534

You can create an UIView extension and use it in your entire application.

extension UIView{
var parentViewController: UIViewController?{
    var parentResponder: UIResponder? = self
    while parentResponder != nil {
        parentResponder = parentResponder!.next
        if parentResponder is UIViewController{
            return parentResponder as! UIViewController!
        }
    }
    return nil                                                      
   }                                                                 
}

And use it like this

if parentViewController?.isKind(of: screen1vc){

}

Upvotes: 1

Dharma
Dharma

Reputation: 3013

You can do by two ways

1.To getting previous one from navigationController viewControllers array by doing like below.

if let navController = self.navigationController, navController.viewControllers.count >= 2 {
     let viewController = navController.viewControllers[navController.viewControllers.count - 2]
}

2.The another way by creating string variable in commonVC & fill required values while pushing to viewController.You can do something like below

Create a variable in commonVC

 var isFrom:String = ""

assign value before pushing using commonVc object.

 commonvc.isFrom = "screen1VC"
 self.navigationController?.pushViewController(commonvc, animated: true)

Now in commonVC

if (self.isFrom == "screen1VC") 

{

}   else if (self.isFrom == "screen2VC"){

}

Upvotes: 0

Yogi
Yogi

Reputation: 3588

In swift, if you are in the navigationcontroller stack then

navigationcontroller.viewControllers[navigationcontroller.viewControllers.count - 2]

will give you the UIViewController object you need.

In case you are presenting the view controller then self.presenting will do the job for you.

Hope this helps!

Upvotes: 0

srijansrv
srijansrv

Reputation: 5

You can have a parent controller property in common VC. set it in the prepareFor segue method in the screen1vc and screen2vc with self.

Now, in commmonVC, just check

if ([parentController iskindOfClass: screen1vc]) 
{

}else if ([parentController iskindOfClass: screen2vc]){

}

This should work.

Upvotes: 0

Explorer.AS
Explorer.AS

Reputation: 161

 NSUInteger numberOfViewControllersOnStack = [self.navigationController.viewControllers count];
 UIViewController *parentViewController = self.navigationController.viewControllers[numberOfViewControllersOnStack - 2];

    if ([parentViewController isKindOfClass: screen1vc]){

    }else if ([parentViewController isKindOfClass: screen2vc]){

    }

Upvotes: 0

Related Questions