user3348175
user3348175

Reputation:

How can I get the name of presentingViewController around a UINavigationController in Swift 2?

In my application I have a modal view that can be called from two different viewControllers. Let’s call them MainViewController and DetailViewController. This modal view is also embedded in a UINavigationController.

I’m trying to do an if/else statement based off of which ViewController triggered the modal to appear.

The code that I currently have in the modal's ViewController is:

if presentingViewController is DetailTableViewController {

        //Update a current Distance
        if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
            distance.name = name!
            distance.length = length!
            if let distanceImage = distanceImageView.image {
                distance.image = UIImagePNGRepresentation(distanceImage)
            }

            do {
                try managedObjectContext.save()
            } catch {
                print(error)
                return
            }
        }
    } else if presentingViewController is ViewController {

        //Save a new Distance
        if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
            distance = NSEntityDescription.insertNewObjectForEntityForName("Distance", inManagedObjectContext: managedObjectContext) as! Distance
            distance.name = name!
            distance.length = length!
            if let distanceImage = distanceImageView.image {
                distance.image = UIImagePNGRepresentation(distanceImage)
            }

            do {
                try managedObjectContext.save()
            } catch {
                print(error)
                return
            }
        }
    }

It appears though that the result of presentingViewController is only returning the UINavigationController that it’s embedded in. Is there some way that I can get around that controller and test against the view that segues to the UINavigationController in the first place?

I'm working with iOS 9 and Swift 2. Thanks in advance for any help!

Upvotes: 1

Views: 1180

Answers (3)

VIP-DEV
VIP-DEV

Reputation: 221

I guess what you are presenting is navigationcontroller. So one can test for navigationcontrollers property i.e viewControllers which will return an array of view controllers i.e either MainViewController and DetailViewController. So here one can use filter operation for array to check the controller one wants to test.As shown below.

let controllerList = (presentingViewController as? UINavigationController).viewControllers
let isControllerExsist = controllerList.filter{$0 is MainViewController}
if isControllerExsist.count>0
{
print("isMainViewCntroller")
}

Upvotes: 1

jasonnoahchoi
jasonnoahchoi

Reputation: 871

if it's between specifically only two view controllers you can pass a Bool to the destination view controller.

In your firstVC:

let destinationVC = DestinationVC() 
// or storyboard.instantiateViewControllerWithIdentifier("destinationVC") as! DestinationVC
destinationVC.isDetail = true
showViewController(destinationVC)

In your destinationVC, in your -viewDidLoad:

if isDetail {
    if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
        distance.name = name!
        distance.length = length!
        if let distanceImage = distanceImageView.image {
            distance.image = UIImagePNGRepresentation(distanceImage)
        }

        do {
            try managedObjectContext.save()
        } catch {
            print(error)
            return
        }
    }
} else {
    //Save a new Distance
    if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
        distance = NSEntityDescription.insertNewObjectForEntityForName("Distance", inManagedObjectContext: managedObjectContext) as! Distance
        distance.name = name!
        distance.length = length!
        if let distanceImage = distanceImageView.image {
            distance.image = UIImagePNGRepresentation(distanceImage)
        }

        do {
            try managedObjectContext.save()
        } catch {
            print(error)
            return
        }
    }
}

and in your secondVC set the bool isDetail to false

Upvotes: 0

tbondwilkinson
tbondwilkinson

Reputation: 1087

I would recommend using prepareForSegue and populate a field on that view controller you're segueing to (make it a weak var!). If the view controller you're segueing to is the navigation controller, which it probably is, you'll need to make a custom navigation controller with the var, and then the presentingViewController will be this custom type, and will have the presenting view controller. Either that or in the navigation controller's prepareForSegue populate another var on your modal view.

Upvotes: 0

Related Questions