2016-10-02 91 views
0

假設我在Main.storyboard中有三個視圖控制器。三個中的兩個,vc_loginvc_studyDesc使用具有「模態存在」選項的UIButton加載其他視圖控制器。如何以編程方式加載數據到下一個視圖控制器?

另一個vc_signup有一個UIButton,它可能會返回到前一個控制器。爲了實現這一點,我使用了以下方法:

vc_studyDesc的標識符爲studyDesc;我讓它通過它的標識符vc_signup。以相同的方式,vc_login具有login作爲標識符。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if sender as! UIButton == qnaSignUp { 
     let signup = segue.destinationViewController as! vc_signup 
     signup.latestVC = "studyDesc"}} 

這一個是在UIViewControllervc_signup。通過參考字符串latestVC,該方法確定哪個VC繼續前進。

@IBAction func backBtnClick(sender: UIButton) { 
    print("latestVS: \(latestVC)") 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier(latestVC) 
    vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve 
    print("check check") 
    self.presentViewController(vc, animated: true, completion: nil)} 

我的問題是,當vc_studyDescvc_signup稱爲應用程序被終止。我發現這是因爲我錯過了一個必須在vc_signup中加載的重要變量。

vc_studyDesc有一些數據在加載時從Firebase引用。我是通過加載一個變量postID從之前的vc到vc_studyDesc;這是vc_list

所以我剛剛使用NSUserdefaults.standardUserDefaults()保存了postID。它解決了,但我想知道是否有任何方法可以使用我在vc_signup中使用的方式傳遞數據。

據我所知,我找不到任何方式將數據傳入vc_studyDesc.swift;因爲vc是通過它的標識符來選擇的。

我可以按照我想要的方式傳遞我想要的變量嗎?添加標籤將不勝感激!

回答

1

所以這個設計有幾個問題。

當您實例化一個viewController時,您正在創建該類的新實例,並將其添加到堆棧中。想像一副撲克牌一樣,你從一張牌開始,然後添加或移除它們,頂牌是可見的vc。當你回到studyDesc時,你正在實例化並呈現它,所以你的堆棧中將會有3個VC,其中兩個是studyDesc(你開始使用的那個,以及當你嘗試返回時添加的那個)

從堆棧中刪除VC可以使用

dismissViewController(animated: true, completion: nil) 

,或者如果你有一個導航控制器的風險投資,你可以順便viewControllers之間的信息方面使用

popViewControllerController(animated: true, completion: nil) 

,如果信息是在你用來展示你的新控制器的VC可以使用像你已經擁有的prepareForSegue。要傳回信息,您應該使用委託模式。因此,爲了實現在這種情況下,委託模式,你會做到以下幾點:

聲明的協議(不是你的班級裏面,上面有,但下方的進口的)

protocol SignUpDelegate { 
    signInCompleted(infoToPass: AnyObject) 
} 

然後讓你的studyDesc類符合本協議並實現功能signInCompleted

StudyDescVC: UIViewController, SignUpDelegate { 

    func signInCompleted(infoToPass: AnyObject) { 
     // do what you want with the info here 
    } 

} 

然後在你的signUpVc添加一個變種委託(將被用來調用signInCompeleted功能)

class SignInVC: UIViewController { 

    var delegate: SignUpDelegate! 

    func finishedSigningIn() { 
     delegate.signInCompleted(infoToPass: //yourinfo) 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 

,然後在prepareForSegue設置委託

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if sender as! UIButton == qnaSignUp { 
    let signup = segue.destinationViewController as! vc_signup 
    signup.delegate = self 
    } 
} 
0
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier(latestVC) as! YOUR_VIEW_CONTROLLER_NAME 
    vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve 
    vc.name = "Andrew" 
    print("check check") 
    self.presentViewController(vc, animated: true, completion: nil) 


    //set a variale or property to your viewController 

    class YOUR_VIEW_CONTROLLER_NAME: UIViewController { 
    var name: String? 

} 
相關問題