呈现导航控制器的ViewController

时间:2018-04-04 13:12:23

标签: ios swift uiviewcontroller uinavigationcontroller

所以我将一个ViewController(A)嵌入到导航控制器(B)中(它是唯一的一个)。导航控制器由自定义ViewController(C)呈现(编辑:已解决 - 这个也嵌入在导航中,它不在我的其他项目中)(我的应用程序的主要部分)。 在我的上一个项目中,我可以使用如下的presentsViewController从ViewController C中访问ViewController A:

let presentedBy = presentingViewController as! TableViewController

但不知何故在另一个具有相同设置的项目中执行此操作会导致错误。还有另一种方法可以达到这个目的吗?

这是运行的完整方法:

@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)
}

如果它有助于这是输出日志:

  

objc [10721]:类VCWeakObjectHolder在两者中实现   /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)和   /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)。将使用两者之一。哪一个未定义。   2018-04-04 15:03:20.379905 + 0200 WHIR [10721:1342292] [MC]系统组   systemgroup.com.apple.configurationprofiles路径的容器是   /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]在线阅读   私人有效用户设置。交易成功保存!   父:可选()呈现:   可选()父母提出:   父的nil parent:nil无法转换类型的值   'UINavigationController'(0x107524400)到'WHIR.TableViewController'   (0x1043776a0)。 2018-04-04 15:03:24.041161 + 0200 WHIR [10721:1342292]   无法将类型'UINavigationController'(0x107524400)的值转换为   'WHIR.TableViewController'(0x1043776a0)。 (LLDB)

故事板截图(左起第二个是右边的ViewController C是ViewController右边的第二个NavController B)enter image description here

3 个答案:

答案 0 :(得分:1)

你可以尝试

let presentedNav = presentingViewController as! UINavigationController

let vc  = presentedNav.viewControllers[0] as! TableViewController

答案 1 :(得分:1)

试试这个

@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) 
    }
}

答案 2 :(得分:1)

让navigationController = UINavigationController()

navigationController.viewControllers [0]

这就是你使用navigationcontroller中的viewcontrollers访问数组的方法。从我在错误中看到的问题是将其从其他项目(引用或其他)复制的问题

相关问题