![](/img/trans.png)
[英]How can I push the next view controller on a navigation controller programmatically?
[英]How can I pass data to the next view controller when it is loaded programmatically?
假設我在Main.storyboard中有三個視圖控制器。 這三個中的兩個vc_login
和vc_studyDesc
使用帶有'present modally'選項的UIButton加載另一個視圖控制器。
另一個vc_signup
具有UIButton,該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"}}
這是在vc_signup
的UIViewController
類中。 通過引用字符串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_studyDesc
調用vc_signup
時,應用程序終止。 我發現這是因為我錯過了必須在vc_signup
加載的重要變量。
vc_studyDesc
有一些要從Firebase引用的數據。 我是通過從先前的vc加載一個變量postID
到vc_studyDesc
來vc_studyDesc
; 這是vc_list
。
所以我只是使用NSUserdefaults.standardUserDefaults()
保存postID
。 它已解決,但我想知道是否有任何方法可以使用vc_signup
使用的方式傳遞數據。
據我所知,我找不到任何將數據傳遞到vc_studyDesc.swift
; vc由其標識符選擇。
我可以按我想要的方式傳遞想要的變量嗎? 並添加標簽將不勝感激!
因此,此設計存在兩個問題。
當實例化一個viewController時,您將創建該類的新實例,並將其呈現出來將其添加到堆棧中。 可以將堆棧想象成一副紙牌,您從一張紙牌開始,然后添加或刪除它們,最上面的紙牌是可見的vc。 當您回到studyDesc時,您將實例化並呈現它,因此您的堆棧中將有3個VC,其中兩個是studyDesc(一個是您開始使用的,另一個是您嘗試返回時添加的)
要從堆棧中刪除VC,可以使用
dismissViewController(animated: true, completion: nil)
或者如果您在導航控制器中具有VC,則可以使用
popViewControllerController(animated: true, completion: nil)
就在viewController之間傳遞信息而言,如果該信息位於您用於呈現新控制器的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中添加一個var委托(將用於調用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
}
}
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?
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.