【问题标题】:iOS New-Contact-Style Segue in SwiftSwift 中的 iOS 新接触式 Segue
【发布时间】:2018-05-29 22:10:37
【问题描述】:

我正在尝试模拟两个视图控制器之间的 iOS 联系人。

我有一个简单的Person 类,由:

class Person {
    var name = ""
}

还有一个UIViewController,其中包含Person的数组,该数组嵌入在UINavigationController中:

class PeopleViewController: UIViewController {

    var people = [Person]()
    var selectedPerson: Person?

    switch segueIdentifier(for: segue) {
    case .showPerson:
        guard let vc = segue.destination as? PersonViewController else { fatalError("!") }
        vc.person = selectedPerson 
    }

}

这个控制器使用 Show segue 到PersonViewController 来显示selectedPerson

class PersonViewController: UIViewController {
    var person: Person!
}

PeopleViewController 还可以将新的Person 添加到Person 的数组中。 NewPersonViewController模态呈现,但是:

class NewPersonViewController: UIViewController {
    var person: Person?
}

如果添加了新的 Person,我希望 NewPersonViewController 关闭,但 显示 PersonViewController 中的新 Person 是导航堆栈的一部分。我对此的最佳猜测是:

extension NewPersonViewController {
    func addNewPerson() {
        weak var pvc = self.presentingViewController as! UINavigationController
        if let cvc = pvc?.childViewControllers.first as? PeopleViewController {
            self.dismiss(animated: false, completion: {
                cvc.selectedPerson = self.person
                cvc.performSegue(withIdentifier: .showPerson, sender: nil)
            }
        }
    }
}

但是,(1)我不太乐意将向下转换为UINavigationController,因为我预计self.presentingViewControllerPeopleViewController 类型? (2),在我使用weak var pvc = self.presentingViewController 处理pvc 而不是cvc 时,闭包中是否存在内存泄漏?或者,最后 (3) 有没有更好的方法来做到这一点?

非常感谢您的任何帮助、建议等。

【问题讨论】:

  • 如果你正在构建一个联系人列表类型的东西,使用 tableview 控制器不是最好的吗?我会做的就像联系人列表一样。使用加号按钮并弹出添加联系人视图控制器,然后如果要保存它,请添加完成按钮提交到数据库(即人员列表)。关闭 Add Contact vc,当您返回表格视图时,重新加载数据。
  • @u84six 您好,感谢您的建议。我使用表格视图来表示people 数组。当您在表格视图中单击 Person 时,它会成为选定的 Person 并转到 PersonViewController。单击“完成”按钮时,我想直接从NewPersonViewController 转到PersonViewController

标签: ios swift segue uipresentingcontroller


【解决方案1】:

(1) 我对强制向下转换为 UINavigationController 不太满意,因为我预计 self.presentingViewController 的类型是 PeopleViewController?

向下转换没有错。我肯定会删除强制展开。

(2),闭包中是否存在内存泄漏,因为我对 pvc 使用了 weak var pvc = self.presentingViewController 而不是 cvc?

我想,没有。

(3) 有更好的方法吗?

您可以从 NewContactVC 中展示新添加的联系人。当你要解雇时,在presentingVC上调用dismiss。

// NewPersonViewController.swift
func addNewPerson() {
// New person is added
// Show PeopleViewController modally
}

注意:以这种方式使用 presentingViewController 将关闭前两个/一个 Modal(s)。您只会看到顶视图控制器被解雇。 如果您无法确定要使用多少个模式,您应该寻找不同的解决方案或可能重新设计导航流程。

// PeopleViewController.swift
func dismiss() {
    if let presentingVC = self.presentingViewController?.presentingViewController {
       presentingVC.dismiss(animated: true, completion: nil)
    } else {
       self.dismiss(animated: true, completion: nil)
    }
}

【讨论】:

  • 感谢您的回答。你知道为什么presentingViewControllerUINavigationController而不是PeopleViewController吗?
  • @Alex 当你呈现视图时,它是从根视图控制器呈现的。如果您使用的是选项卡,它将是 tabviewcontrollers,因为它是 rootviewcontroller
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
相关资源
最近更新 更多