2016-12-26 53 views
1

我使用這個打開另一個視圖控制器:傳遞字符串/對象值到另一個視圖控制器

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil) 
let homeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "IssueViewController") 
self.present(homeViewController, animated: true, completion: nil) 

伴隨着這一點,我需要通過一個Person對象和一個字符串值,以第二的ViewController。

struct Person { 
    var Name: String 
    var Details: String 
} 

我需要做什麼才能將Person對象附加到我的現有代碼?

編輯:這是第二的ViewController

我試圖從該視圖檢索值

class IssueViewController: UIViewController { 

    var person: Person = Person(); 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

} 
+0

問題視圖控制器必須具有propery Person,並且您需要的所有內容都設置爲您的對象的homeViewController.person –

回答

1
在第一控制器

//改變

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main) 
let homeViewController: IssueViewController = mainStoryboard.instantiateViewController(withIdentifier: "IssueViewController") as! IssueViewController 
homeViewController.person = Person(Name:"ABC",Details:"XYZ") 
homeViewController.bindWithData(yourStringObject) 
self.present(homeViewController, animated: true, completion: nil) 

//改變在第二視圖控制器中

class IssueViewController: UIViewController { 

var person: Person = Person(Name:"",Details:""); 

override func viewDidLoad() { 
    super.viewDidLoad() 
    print(person.Name) 
    print(person.Details) 
} 
func bindWithData(yourStringObject:String){ 
//your code here. 
} 
} 
+0

當我將該人設置爲homeViewController時,出現此錯誤:'類型'UIViewController'的值沒有成員'person ' – Dinuka

+0

你有沒有定義struct Person? –

+0

嘿抱歉有一個問題,我已修復它的編輯請檢查代碼。 –

相關問題