2016-11-14 57 views
0

我有2個控制器:Controller1和Controller2作爲一個子視圖。 Controller1具有附加的UISearchBar,OnClick Controller2顯示爲帶有TableView的SubView。由於在搜索欄,用戶類型,我可以使用這個IOS Swift如何獲得SearchBar文本值查看1到查看2

// Controller1 
    @IBOutlet weak var mySearch: UISearchBar! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    mySearch.delegate = self 
    // Do any additional setup after loading the view. 
} 
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){ 
     if let text = searchBar.text { 
      let search = text.trimmingCharacters(in: .whitespaces) 
      _ = search 

     } 
    } 

現在我最大的問題是讓我在得到SEARCHTEXT值並將其傳遞到控制器2得到的結果,我怎麼能去這樣做? Controller2執行HTTP Post請求,並使用Controller1中的SearchText的值。這是在控制器2

class Controller2: UIViewController,UITableViewDataSource { 
@IBOutlet weak var TableSource: UITableView! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    TableSource.dataSource = self 

    // I would like to get value of SearchText here so that I can 
    // send it as a parameter in my HttpPost request 

    session.dataTask(with:request, completionHandler: {(data, response, error) in 
     if error != nil { 

     } else { 
      do { 

       // get Json Data 

      } catch let error as NSError { 
       print(error) 
      } 
      print(self.locations) 
     } 

    }).resume() 



} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int { 
    return locations.count 
} 





func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Registration_Search", for: indexPath) 

    return cell 
} 



} 

這是在控制器1和上的UISearchBar的水龍頭,這是我的代碼怎麼弄控制器2作爲一個子視圖

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 
      Location_Search.showsCancelButton = true 

     let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Controller2") as! Controller2 
     self.addChildViewController(Popup) 
     Popup.view.frame = self.Controller2.frame 
     self.view.addSubview(Popup.view) 
     Popup.didMove(toParentViewController: self) 
     return true 
    } 
+0

視圖控制器的層次結構如何?兩者是否有不同的導航控制器,或相同的? – Sethmr

+0

他們只是2個不同的ViewControllers沒有導航控制器 – user1591668

+0

兩個不同的視圖控制器在哪裏?他們必須處於某種或某種形式的層次結構中,以便您同時需要訪問他們兩個。 – Sethmr

回答

0

試試這個:

func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 
      Location_Search.showsCancelButton = true 

     let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Controller2") as! Controller2 
     self.addChildViewController(Popup) 
     Popup.view.frame = self.Controller2.frame 
     Popup.textFromSearch = mySearch.text 
     self.view.addSubview(Popup.view) 
     Popup.didMove(toParentViewController: self) 
     return true 
    } 

和在您的控制器2緊隨IBOutlet add var:

var textFromSearch = ""