How to properly use popToViewController?
You can't instantiate a new instance of the view controller you are trying to pop to; you need to pop to the actual instance that is in the navigation stack.
If your menu is the root of your navigation stack then you can use popToRootViewController
. If it is somewhere else in the stack then you either need to hold a reference to it or you can iterate through the navigation controller's viewControllers
array to find it and then pop to it.
if let navController = self.navigationController {
for controller in navController.viewControllers {
if controller is MenuController { // Change to suit your menu view controller subclass
navController.popToViewController(controller, animated:true)
break
}
}
}
popTOViewController
Try this.
Where I have written SeeMyScoresViewController you should write your View Controller class on which you have to go.(eg. Class of Home)
NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
id obj=[viewControllers objectAtIndex:i];
if([obj isKindOfClass:[SeeMyScoresViewController class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
popToViewController isn't popping all of the view controllers
Your confusion may simply be the way you are trying to "check" that the VCs are "popped".
Suppose you have gone:
root->TableView->A->B->A->B->B->B->`
At that point, the only VC
that is visible is the last instance of A
. So when you call
navController.popToViewController(controller, animated: true)
viewWillDisappear()
will only be called on the last instance of A
- none of the other VC
instances will "disappear" because they are not visible.
If you want to confirm the other VCs in the stack are being "removed", put this in each view controller:
deinit() {
print("I'm being removed:", self)
}
The other part of the question - do you want to animate through the process? So you would actually see the VCs "walk back up the stack"? If so, follow @FryAnEgg's link to Completion block for popViewController
Swift 3: popToViewController not working
popToViewController
not work in a way you are trying you are passing a complete new reference of FirstTableViewController
instead of the one that is in the navigation stack. So you need to loop through the navigationController?.viewControllers
and find the FirstTableViewController
and then call popToViewController
with that instance of FirstTableViewController
.
for vc in (self.navigationController?.viewControllers ?? []) {
if vc is FirstTableViewController {
_ = self.navigationController?.popToViewController(vc, animated: true)
break
}
}
If you want to move to First Screen then you probably looking for popToRootViewController
instead of popToViewController
.
_ = self.navigationController?.popToRootViewController(animated: true)
pass variable value by popToViewController in swift
Try this.
let viewControllers = self.navigationController!.viewControllers
for var aViewController in viewControllers
{
if aViewController is LocationVC
{
let aVC = aViewController as! LocationVC
aVC.NameofCircle = CName
_ = self.navigationController?.popToViewController(aVC, animated: true)
}
}
another choice To pass value to Root ViewController
if let myController = self.navigationController?.viewControllers[0] as? LocationVC
{
myController.NameofCircle = CName
_ = self.navigationController?.popToViewController(myController, animated: true)
}
popToViewController is not working but popViewController does
try this:-
for obj in (self.navigationController?.viewControllers)! {
if obj is TestViewController {
let vc2: TestViewController = obj as! TestViewController
vc2.data = data
self.navigationController?.popToViewController(vc2, animated: true)
break
}
}
Make sure your view controller is added on navigationcontroller stack.
How to pass data when calling popToViewController?
For some reason the UIImageView
on my 2nd UIViewController
is not updating the image because I passed the UIImage
in the dataPass
model class. I changed the code like this :
func showSuccessPopUp() { // Based on DonMag's answer
let popupVC = VehiclePhotoSuccessPopUpVC(nibName: "VehiclePhotoSuccessPopUpVC", bundle: nil)
popupVC.modalPresentationStyle = .overCurrentContext
popupVC.okTapped = { (sender) in
// we want to find the Second VC in the stack, which will be [1]
guard let destinationVC = self.navigationController?.viewControllers[1] as? RestructureFormVC else {
fatalError("Second VC in navigation stack is NOT a RestructureFormVC")
}
destinationVC.source = "previewPhoto" // A string for knowing where the source is in the 2nd UIViewController
destinationVC.assetPhoto = self.photoImage // Set the image immediately without using the dataPass model class
self.navigationController?.popToViewController(destinationVC, animated: true)
}
self.present(popupVC, animated: false, completion: nil)
}
So here what I did was I just set the UIImage
directly to the assetPhoto
variable in my 2nd controller. And to update the UIImageView
I create a function in my 2nd controller to check whether the assetPhoto
variable is nil or not and call it in viewWillAppear
like this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setAssetImage()
}
func setAssetImage() {
if assetPhoto == nil {
photoParentView.isHidden = true
} else {
photoParentView.isHidden = false
photoImageView.image = assetPhoto
}
}
And it works as I expected to be.
Related Topics
How to Make Both Header and Footer in Collection View with Swift
How to Use Cocoapods When Creating a Cocoa Touch Framework
Ios7 Safari: Saving to Home-Screen and Persist Token
Can't Prevent 'Touchmove' from Scrolling Window on iOS
Nsarray with Nspredicate Using Not In
Xcode Is Creating Generic Xcode Archive Instead of iOS App Archive
Libmobilegestalt Mobilegestaltsupport.M:153 Mobilegestalt.C:550 Xcode Console
How to Stop Uitableview from Clipping Uitableviewcell Contents in iOS 7
Remove Uisegmentedcontrol Separators Completely. (Iphone)
Xcode 4.2 Jumps to Main.M Every Time After Stopping Simulator
How to Debug "Invalid Bundle" Error Which Happens Only After Submitting to App Store
View at the Bottom in a Uiscrollview, with Autolayout
Wkwebview Didn't Finish Loading, When Didfinishnavigation Is Called - Bug in Wkwebview
What's Always_Embed_Swift_Standard_Libraries with Cocoapods, Swift 3 and Xcode 8
Hide Dots from Uipageviewcontroller