Swift popToViewController

Good afternoon guys, I'm learning Swift, I need help here.

The user registers and displays his image. After rejecting the image selection, I would like the ComposeViewController to appear.

Here is the code:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) { let pickedImage:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage //Scale Down Image let scaledImage = self.scaleImageWith(pickedImage, and: CGSizeMake(100,100)) let imageData = UIImagePNGRepresentation(scaledImage) let imageFile:PFFile = PFFile(data: imageData) PFUser.currentUser().setObject(imageFile, forKey: "profileImage") PFUser.currentUser().saveInBackgroundWithTarget(nil, selector: nil) picker.dismissViewControllerAnimated(true, completion: nil) //this is the line seems to have problem. self.navigationController?.popToViewController(ComposeViewController, animated: true) } 

Then I got the following error: ComposeViewController.Type 'does not convert to' UIViewController Expected member name or constructor call after type name

He has a suggestion to fix by putting () after the ComposeViewController, but then he throws more errors after fixing.

Hope someone can help. Thank you !: -)

+9
source share
9 answers

I know this is old, but it is similar to what Sakib said, you cannot resort to the view manager, which does not exist yet.

Many of the answers here seem to be from people who haven't read your question, just a title. I will leave this code here if it helps anyone.

 let vcIndex = self.navigationController?.viewControllers.indexOf({ (viewController) -> Bool in if let _ = viewController as? ComposeViewController { return true } return false }) let composeVC = self.navigationController?.viewControllers[vcIndex!] as! ComposeViewController self.navigationController?.popToViewController(composeVC, animated: true) 
+7
source
 let controllers = self.navigationController?.viewControllers for vc in controllers! { if vc is YourVC { _ = self.navigationController?.popToViewController(vc as! YourVC, animated: true) } } 
+6
source

There is a method that allows you to access the array of all ViewControllers in the current stack, and you can grab the one you want using its index, for example:

 let switchViewController = self.navigationController?.viewControllers[1] as! ComposeViewController self.navigationController?.popToViewController(switchViewController, animated: true) 
+4
source
 if let composeViewController = self.navigationController?.viewControllers[1] { self.navigationController?.popToViewController(composeViewController, animated: true) } 
+3
source

I ended up replacing the following code inside the main view, and it works. I'm not sure if this is correct, could you give me some comments?

 //self.navigationController?.popToViewController(ComposeViewController, animated: true) let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as ComposeViewController self.navigationController?.pushViewController(switchViewController, animated: true) 

I defined "view2" as the identifier of the destination storyboard.

+2
source
Navigation controller

Supports a stack of views that you click. Its like the last in the first turn.

To pop up on the ComposeViewController, this view must already exist in the queue, and you must have a link to it.

You will need to pass an instance of ComposeViewController. for simplicity you can save this link in appdelegate. (this approach is not recommended)

0
source
 for (var i = 0; i < self.navigationController?.viewControllers.count; i++) { if(self.navigationController?.viewControllers[i].isKindOfClass(DestinationViewController) == true) { self.navigationController?.popToViewController(self.navigationController!.viewControllers[i] as! DestinationViewController, animated: true) break; } } 
0
source

In Swift 4.1 and Xcode 9.4.1

Suppose if you switched from the 1st ViewController to the 2nd, and then from the 2nd to the 3rd. Now, if you want to return directly from 3rd to 1, this code is enough.

 if let composeViewController = self.navigationController?.viewControllers[1] {//Here you mention your view controllers index, because navigation controller can store all VC'c in an array. print(composeViewController) self.navigationController?.popToViewController(composeViewController, animated: true) } 
0
source

What seemed more useful to me was to do the first search using viewControllers , so you get the first instance, viewControllers on the stack, without having to guess the actual index.

eg

 let mainViewControllerVC = self.navigationController?.viewControllers.first(where: { (viewcontroller) -> Bool in return viewcontroller is ComposeViewController }) if let mainViewControllerVC = mainViewControllerVC { navigationController?.popToViewController(mainViewControllerVC, animated: true) } 
0
source

Source: https://habr.com/ru/post/981660/


All Articles