So I have an ViewController (A) embedded into an Navigation Controller (B) (it's the only one in it). The navigation controller is getting presented by a custom ViewController (C) (edit: resolved - this one is also embedded in a nav, which it isn't in my other project) (the main one of my app). In my last project I could access the ViewController A from the one from ViewController C by using the presentingViewController like this:
let presentedBy = presentingViewController as! TableViewController
But somehow doing this in another project with the same set up results in an error. Is there another way I can achieve this?
This is the full method that runs:
@IBAction func save(_ sender: Any) {
//text fields cant be nil
guard let title = titleTextField.text, let summary = summarizeTextView.text else { return }
//save item
let item = NSEntityDescription.insertNewObject(forEntityName: "Book", into: managedObjectContext) as? Book
item?.title = title
item?.summary = summary
item?.date = NSDate()
print("Transaction successfully saved!")
//dismiss view
print("Parent: \(parent)")
print("Presenting: \(presentingViewController)")
print("Parents presenting: \(parent?.presentingViewController?.presentingViewController)")
print("parent of parent: \(parent?.parent)")
let presentedBy = presentingViewController as! TableViewController
presentedBy.changeNavTitle()
dismiss(animated: true, completion: nil)
}
And if it helps this is the output log:
objc[10721]: Class VCWeakObjectHolder is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/Frameworks/ViceroyTrace.framework/ViceroyTrace (0x1247d64d0) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/AVConference (0x123902e38). One of the two will be used. Which one is undefined. 2018-04-04 15:03:20.379905+0200 WHIR[10721:1342292] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/bruce/Library/Developer/CoreSimulator/Devices/5E006C1F-F730-46C2-92AC-99DB207A3FBB/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-04-04 15:03:20.380798+0200 WHIR[10721:1342292] [MC] Reading from private effective user settings. Transaction successfully saved! Parent: Optional() Presenting: Optional() Parents presenting: nil parent of parent: nil Could not cast value of type 'UINavigationController' (0x107524400) to 'WHIR.TableViewController' (0x1043776a0). 2018-04-04 15:03:24.041161+0200 WHIR[10721:1342292] Could not cast value of type 'UINavigationController' (0x107524400) to 'WHIR.TableViewController' (0x1043776a0). (lldb)
Storyboard Screenshot (second from left is ViewController C first from right is ViewController A second from right The NavController B) :
You can try
let presentedNav = presentingViewController as! UINavigationController
let vc = presentedNav.viewControllers[0] as! TableViewController
Try this
@IBAction func save(_ sender: Any) {
//text fields cant be nil
guard let title = titleTextField.text, let summary = summarizeTextView.text else { return }
//save item
let item = NSEntityDescription.insertNewObject(forEntityName: "Book", into: managedObjectContext) as? Book
item?.title = title
item?.summary = summary
item?.date = NSDate()
print("Transaction successfully saved!")
//dismiss view
print("Parent: \(parent)")
print("Presenting: \(presentingViewController)")
print("Parents presenting: \(parent?.presentingViewController?.presentingViewController)")
print("parent of parent: \(parent?.parent)")
if let presentedBy = presentingViewController as? UINavigationController, let rootVC = presentedBy.viewControllers.first as? TableViewController {
rootVC.changeNavTitle()
dismiss(animated: true, completion: nil)
}
}
let navigationController = UINavigationController()
navigationController.viewControllers[0]
pretty much this is how u access the array with the viewcontrollers in your navigationcontroller. From what i see in the error the issue is something with copying it from the other project (reference or something)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.