0

I have a search tags view for my app. It allows users to add search tags to make searching easier. The search tag view is all done. But I'm having problems accessing the text from the tags and compiling them together to make one large string. Can someone help me. Here is the code. I took the repo project rrtagcontroller from github and customized it. All i want to do is take the text from all the tags and put them in one big string so that I can pass the data to the next view controller.

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

    }
}
user7222919
  • 149
  • 1
  • 3
  • 10

1 Answers1

1

According to the github page of RRTagController, the two callback blocks will either provide a list of selected and unselected tags or nothing if user cancels. So according to your code, you can access the tags in the blocks as follows:

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
}
Hong Wei
  • 1,397
  • 1
  • 11
  • 16
  • I'm trying to pass data to the next view controller. How do I pass selectedtagsasstrings to the next viewcontroller – user7222919 Jan 02 '17 at 00:04
  • It depends. If you are using storyboards, you can store the selected tags as an instance var, perform the segue programmatically (`performSegue(withIdentifier:sender:)`), set the selected tags within `func prepare(for: UIStoryboardSegue, sender: Any?) `. Alternatively, you can just programmatically init the next view controller, set the selected tags on the view controller and show the next view controller See this: http://stackoverflow.com/a/13980519/1594442 – Hong Wei Jan 02 '17 at 00:25
  • i know how to pass data. but what would the string I'm passing be. how would i declare that. How would I call over all of the selectedtagsasstrings – user7222919 Jan 02 '17 at 00:51
  • You can declare a variable of type [String] on the next view controller class. Then after initializing the next view controller, set it to `selectedTagsAsStrings` – Hong Wei Jan 02 '17 at 01:04
  • To call over that data, use a for in loop – Hong Wei Jan 02 '17 at 01:05
  • Hong Wei, when I try to access tag string in the (func prepare(for: UIStoryboardSegue, sender: Any?)), I get a "use of unresolved identifier 'tag string') error when I try to declare it to this (destinationVC.HomeDescriptiontext = tagString) – user7222919 Jan 02 '17 at 01:06
  • what should i do for this – user7222919 Jan 02 '17 at 01:07
  • what is your suggestion – user7222919 Jan 02 '17 at 01:07
  • Hong Wei, can you make an edit to your code and show me – user7222919 Jan 02 '17 at 01:08
  • You need to store it into an instance variable in the `blockFinish`. – Hong Wei Jan 02 '17 at 01:08
  • can you show me how that would look. Im going to edit my code to show you what I'm talking about. And then can you make a re-edit to show me what your talking about – user7222919 Jan 02 '17 at 01:10
  • Edited the answer – Hong Wei Jan 02 '17 at 01:11
  • is this what your talking about – user7222919 Jan 02 '17 at 01:12
  • self.tagString = tagString – user7222919 Jan 02 '17 at 01:12
  • Yes, because after blockFinish executes, the value would be lost, so you need an instance variable to hold it – Hong Wei Jan 02 '17 at 01:13
  • can you look at my edit to show me where tapstring would go in the override prepare func – user7222919 Jan 02 '17 at 01:13
  • Your code looks alright to me. You just need to declare an instance variable on this class. `var tagString: String?` it is declared optional as user may or may not have selected any tags – Hong Wei Jan 02 '17 at 01:20
  • Hey Hong Wei, I was wondering if you could help me with something else – user7222919 Jan 02 '17 at 04:29