2017-09-13 100 views
0

我曾試着在類似的問題上尋找答案,但我並不是特別有經驗,並且遇到了麻煩,因此任何幫助都將不勝感激!我的情況如下:當我在我的父視圖控制器中按下按鈕時,下面的代碼用於調用一個Child ViewController(順便說一句,Child實際上是一個TableViewController,但它似乎工作正常,「認爲」這是一個正常的視圖控制器):如何在父視圖控制器和子視圖控制器之間傳輸數據

controller = (storyboard?.instantiateViewController(withIdentifier: "People")) 
    addChildViewController(controller!) 
    controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) 
    self.view.addSubview((controller?.view)!) 
    controller?.didMove(toParentViewController: self) 

什麼話,我想是從母公司轉移陣列的兒童,在那裏將被用作TableView中的數據?其次,當我從Child的TableView中選擇一個單元格時,我想將相關信息發送給Parent,並讓Child消失。
在情況下,它是感興趣,我已成功使用,收在不同環境下的兒童(當點擊在在顯示孩子的父母時)以下內容:

  controller?.willMove(toParentViewController: nil) 
      controller?.view.removeFromSuperview() 
      controller?.removeFromParentViewController() 

我真的很感激任何意見,即使它是一個可以幫助的東西的鏈接!

+1

可以傳遞數組就像傳遞任何兩個視圖控制器之間的數據的方式。在'addChildViewController(controller!)'行後面,你可以添加'controller.array = self.array'。並返回,您可以使用委託方法。 – CoderFrom94

+0

您可以將'parentsViewVontroller'和'parentsViewVontroller''''中的屬性設置爲'childViewController'並將值傳遞給此變量,或者您可以創建'childViewController'的委託並且可以接收事件或者可以使用這些塊。 – TheTiger

回答

1

你可以通過從父值子控制器這樣

controller = (storyboard?.instantiateViewController(withIdentifier: "People")) 
    addChildViewController(controller!) 
    controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) 
    controller.tableDataSource = // Pass your required value to child controller 
    self.view.addSubview((controller?.view)!) 
    controller?.didMove(toParentViewController: self) 

現在要傳送回你的選擇值父視圖控制器。爲了這個目的,你擁有了一個打造ChildController一個代表像

@protocol ChildControllerDelegate : class { 
    func selectedValue(Value : String) 
} 

後,使該委託的一個變量ChildController這樣

weak var delegate : ChildControllerDelegate? 

而當rowDidSelect方法中添加以下代碼

if(delegate != nil) { 
    delegate.selectedValue(Value :"Your selected value") 
} 

現在,當您要從ParentController顯示ChildController時,您必須將該對象設置爲ParentController l IKE在此

controller = (storyboard?.instantiateViewController(withIdentifier: "People")) 
    addChildViewController(controller!) 
    controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300) 
    controller.delegate = self 
    self.view.addSubview((controller?.view)!) 
    controller?.didMove(toParentViewController: self) 

,之後只是實現委託方法ParentController像

func selectedValue(Value : String) { 
    // you select val 
} 
+0

輝煌,這工作完美,謝謝:) – jasperthedog

0

您可以:

  • controller爲子視圖控制器相應的類(我在下面使用ChildViewController,但希望你有一個更具描述性的名字);和

  • 將當前視圖控制器(父視圖)中的陣列(我猜你可能調用了people,但在這兩個視圖控制器中使用任何數組名稱)都傳遞給此子視圖控制器。

這樣:

let child = storyboard!.instantiateViewController(withIdentifier: "People") as! ChildViewController 
addChildViewController(child) 
child.people = people 
child.view.frame = ... 
view.addSubview(child.view) 
child.didMove(toParentViewController: self) 

就個人而言,我不會硬編碼子座標就像你在你原來的問題沒有。我將translatesAutoresizingMaskIntoConstraints設置爲false,然後添加適當的前導/尾隨/頂部/底部約束,但這取決於您。在你的例子中看到硬編碼座標太痛苦了。

+0

謝謝你的建議!首先讓事情起作用,然後再進行autoresizing(對此很新穎,所以我想這可能不是正確的做法!) – jasperthedog

0

試試這個。

首先創建公共方法添加和刪除childVC。

For Add childVC。

public class func openChildViewController(parentVC:UIViewController, with childVC:UIViewController){ 

     parentVC.addChildViewController(childVC) 
     childVC.view.frame = parentVC.view.frame 
     parentVC.view.addSubview(childVC.view) 
     parentVC.didMove(toParentViewController: childVC) 
    } 

對於刪除childVC。

public class func removeChildViewController(childVC:UIViewController){ 

     childVC.willMove(toParentViewController: nil) 
     childVC.view.removeFromSuperview() 
     childVC.removeFromParentViewController() 
    } 

以上使用方法。

1.ParentVC.swift

class ParentVC: UIViewController , ChildVCDelegate { 

    var arrType = NSMutableArray() 

    //add ChildVC 
    @IBAction func btnAddChildVC(_ sender: UIButton) { 
     let ChildVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC 
     PickerVC.arrPass = arrType //for data passing create any object in ChildVC for ex. arrPass is NSMutableArray 
     ChildVC.delegate = self 
     openChildViewController(parentVC: self, with: ChildVC) 
    } 

    // MARK: ChildVC Delegate 
    func SetSelectedPickerValue(strSelectOption: String) { 
       print(strSelectOption) 
     } 
    } 

} 

2.ChildVC.swift

class ChildVC: UIViewController{ 

    // MARK: Variable for ParentVCData Passing 
    var arrPass = NSMutableArray() 

    override func viewWillAppear(_ animated: Bool) { 
     print(arrPass) 
    } 

    //Remove ChildVC 
    @IBAction func btnRemoveChildVC(_ sender: UIButton) { 
     self.delegate?.SetSelectedPickerValue!(strSelectOption: 「any String you pass ChildVC To ParentVC」) 
     removeChildViewController(childVC: self) 
    } 
} 
// MARK: Create Delegate Method 
@objc protocol ChildVCDelegate{ 
    @objc optional func SetSelectedPickerValue(strSelectOption:String) 
} 
相關問題