0

I have my project setup like this:

I have a custom Tab Navigation Controller. In the top bar of the Custom Tab Navigation Controller is a text field. This text field changes according to the 4 main views of the app (4 tab buttons).

What I am trying to do is use the text field as a search bar by passing whatever is typed into the text field into the searchView However, I am having trouble passing the textfield.text from the Navigation Controller into searchView. I have a picture below that illustrates this more clearly. enter image description here

The searchView has the search function taken care of. All I am trying to do is pass with textfieled.text value to the searchView whenever it is changed

nayem
  • 7,285
  • 1
  • 33
  • 51
ethanfox27
  • 890
  • 1
  • 9
  • 25
  • 1
    If I understood, you want just to detect input text on uitextfield and call a function ? Have you tried textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged) ? – GIJOW May 04 '17 at 22:57
  • No, I have not. Does that allow me to pass whatever is typed into the text field into the search view? The "First View" and "Search View" are view controllers. – ethanfox27 May 04 '17 at 23:01
  • have a look here http://stackoverflow.com/questions/28394933/how-do-i-check-when-a-uitextfield-changes – GIJOW May 04 '17 at 23:03
  • That's sort of what I am looking for. But what I am trying to do is detect if the text field is being edited which is what you showed me. Then I want to be able to transfer the value of text field from the custom navigation controller to the search view controller whenever a change is made. @GIJOW – ethanfox27 May 04 '17 at 23:30

2 Answers2

1

You can do something like that. In your FirstViewController

 func textFieldDidChange(textField: UITextField){

   let search_screen = SearchViewController()
   search_screen.search_string = self.textField.text
   self.navigationController?.pushViewController(search_screen, animated: true)
}

In case of storyboards

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{    
    if (segue.identifier == "Your_SearchViewController_identifier") {
        let search_screen = segue.destinationViewController as! 
         SearchViewController 
        search_screen.search_string = self.textField.text           
    }

}

And In your SeacrchViewController's viewDidLoad:

 override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.text = self.search_string
 }
Umair
  • 1,203
  • 13
  • 15
  • This only seems to work when the text field is initially tapped, but then does nothing once I start typing. I know this because it adds a blank line to the console when I print it from the searchView. This does work a lot better than delegates and all that crap so thank you very much. I'll give you +1 for this. – ethanfox27 May 05 '17 at 01:07
  • No, not yet @GIJOW I'm still trying to figure out how to have the changing value of the text field to transfer over to the search view. – ethanfox27 May 05 '17 at 11:55
  • Ok, what is weird is that this causes the search_string to be printed every time the textfield.text is updated. However, the value of the text field does not get does not get passed on? Would an embed segue be needed for something like this? I changed the func "textFieldDidChange" to "textFieldShouldEndEditing" and added a target to the text field in viewDidLoad. – ethanfox27 May 05 '17 at 12:48
  • The value will not be passed as the variable was set on viewDidLoad. A way (at least with this piece of code) I have imagined is to use Notification to get updated every each time you change the textField. I will post my answer, but as I said, I just imagined that – GIJOW May 05 '17 at 15:44
1
// global 
let handleTextChangeNotification = "handleTextChangeNotification"

your FirstViewController

override func viewDidLoad() {
      super.viewDidLoad()
      textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
}


func textFieldDidChange(textField: UITextField){
       NotificationCenter.default.post(name: NSNotification.Name(rawValue: handleTextChangeNotification), object: nil, userInfo: ["text":textField.text])
       let search_screen = SearchViewController()

       self.navigationController?.pushViewController(search_screen, animated: true)
    }

SeacrchViewController's

 override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(SeacrchViewController.handleTextChange(_:)),
                                           name: NSNotification.Name(rawValue: handleTextChangeNotification),
                                           object: nil)
 }

func handleTextChange(_ myNot: Notification) {
   if let use = myNot.userInfo {
       if let text = use["text"] {
                 // your search with 'text' value
            }
       }
    }
}
GIJOW
  • 2,307
  • 1
  • 17
  • 37