Reputation: 69
So I have 3 VC
ViewController1,ViewController2,ViewController3
Now I have a button in ViewController2
and ViewController3
, which on tapping navigates to ViewController1
there is a slight change in the UI of when navigating from vc2 and when from vc3.
So I want to know the best practice of handling this problem ie. how would I know from which vc have I reached to VC1
.?
Upvotes: 0
Views: 96
Reputation: 9503
You can either use a flag or enum. I suggest for enum bcz sometimes in future you might push from multiple controllers to VC1. Its always handy to use a enum.
- With flag
class ViewController1: UIViewController {
// default value is false bcz if you forgot to assign this value then atleast your app won't crash.
var isFromVC2 : Bool = false
:
:
}
Use -> In your VC1 file
if isFromVC2 {
// Do code for VC2
}
else {
// Do code for VC3
}
- With Enum
enum ComingFrom {
case VC3
case VC2
}
class ViewController: UIViewController {
// default value VC2
var whichController : ComingFrom = .VC2
:
:
}
Use
switch whichController {
case .VC2:
// for vc2 Code
case .VC3:
// for VC3 Code
default:
// If you forget to assign `whichController` or there will be new condition in future
}
Edit : How to assign whichController
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
vc.whichController = .VC2
self.navigationController?.pushViewController(vc, animated: true)
Upvotes: 2
Reputation: 706
Add unique Bool
variable so you can understand where to come to this controller.
Make flag false
in viewWillAppear
because every time it should be updated its a simplest way that you can achieve.
class ViewController1: UIViewController {
var isFromVC2 = false
var isFromVC3 = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
isFromVC2 = false
isFromVC3 = false
}
}
class ViewController2: UIViewController {
//You can call this function from where you want otherwise you can make it global.
func navigateToVC1() {
let viewController1 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
viewController1.isFromVC2 = true
self.navigationController?.pushViewController(viewController1, animated: true)
}
}
class ViewController3: UIViewController {
func navigateToVC1() {
let viewController1 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
viewController1.isFromVC3 = true
self.navigationController?.pushViewController(viewController1, animated: true)
}
}
Upvotes: 1
Reputation: 1097
In your VC1, create a variable as vcNames.
class VC1: UIViewController {
var vcNames = ""
override func viewDidLoad() {
super.viewDidLoad()
//Check your vc's with vcNames.
}
}
Now while pushing to vc1 from VC2 or VC3, Just pass your current vc name with created variable.
let tempVC1 = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC1") as? VC1
tempVC1?.vcNames = "vc2" //Assign your vc name here
self.navigationController?.pushViewController(tempVC1!, animated: true)
Upvotes: 1