Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift popToViewController

Tags:

swift

Good day guys, I'm learning Swift, needed some help here.

The user are signing up and selected their image. Upon dismissing the image picker, I would like to have the ComposeViewController 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 these error: ComposeViewController.Type' is not convertible to 'UIViewController Expected member name or constructor call after type name

It has suggestion to fix by putting () after ComposeViewController but then it gives out more errors after fixing.

Hope someone could help. Thanks! :-)

like image 872
bobster Avatar asked Jan 28 '15 10:01

bobster


People also ask

What is a UIViewController?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.


2 Answers

let controllers = self.navigationController?.viewControllers
              for vc in controllers! {
                if vc is YourVC {
                  _ = self.navigationController?.popToViewController(vc as! YourVC, animated: true)
                }
             }
like image 60
Toqir Ahmad Avatar answered Sep 22 '22 23:09

Toqir Ahmad


I know this is old, but it's like what Saqib said, you can't pop to a viewcontroller that doesn't exist yet.

A lot of the answers here seem to be from people that didn't read your question, just the title. I'll leave this code here in case 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)
like image 43
Hanny Avatar answered Sep 22 '22 23:09

Hanny