从标签视图访问文本



我的应用程序有一个搜索标签视图。它允许用户添加搜索标签以使搜索更容易。搜索标记视图已完成。但是我在从标签访问文本并将它们编译在一起以形成一个大字符串时遇到了问题。有人可以帮我吗?这是代码。我从github获取了repo项目rrtagcontroller并对其进行了自定义。我只想从所有标签中获取文本并将它们放在一个大字符串中,以便我可以将数据传递给下一个视图控制器。

override func viewDidAppear(_ animated: Bool) {
    let tag = ["Macbookpro13inch"]
    RRTagController.displayTagControllerAsAChild(self, frame: CGRect(x: 0.0, y: 161.0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 161), tagsString: tag, blockFinish: { (selectedTags, unSelectedTags) -> () in
    }) { () -> () in
    }
}
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let vc = segue.destination
    vc.transitioningDelegate = transition
    vc.modalPresentationStyle = .custom
    if (segue.identifier == "ToHomeRoomDetailsViewController"){
        var destinationVC:HomeRoomDetailsViewController = segue.destination as! HomeRoomDetailsViewController

        destinationVC.HomeDescriptiontext = tagString
    }
}

根据 RRTagController 的 github 页面,这两个回调块要么提供选定和未选择标签的列表,要么如果用户取消,则什么都不提供。因此,根据您的代码,您可以按如下方式访问块中的标签:

RRTagController.displayTagControllerAsAChild(self, frame: CGRect(x: 0.0, y: 161.0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 161), tagsString: tag, blockFinish: { (selectedTags, unSelectedTags) -> () in
    //map the selectedTags into an array of strings
    let selectedTagsAsStrings = selectedTags.map { $0.textContent }
    //join the strings into one comma-separated string
    let tagString = selectedTagsAsStrings.joinWithSeparator(",")
    //do everything else with tagString
    //for e.g., store it locally
    self.tagString = tagString
}) { () -> () in
    //user did not select any tag, remember to handle this as well
}

最新更新