removeReview我的設計目的是在按下按鈕時顯示彈出螢屏。
removeReview按鈕放置在ReviewItem.swift.
下面是ReviewItem.swift我寫的部分代碼。(型別別是UITableViewCell)
weak var viewController: UIViewController?
var passwords: String = ""
var reviewNum: String = ""
@IBAction func removeReview(_ sender: Any) {
print("ReviewItem - removeReview function called")
print("Passwords: \(passwords)")
print("Review_number: \(reviewNum)")
let PopUpVC = PopUpVC()
PopUpVC.pw = self.passwords
PopUpVC.reviewDoc = self.reviewNum
print(PopUpVC.pw)
print(PopUpVC.reviewDoc)
let storyboard = UIStoryboard.init(name: "PopUp", bundle: nil)
let alertPopUpVC = storyboard.instantiateViewController(withIdentifier: "VerifyPasswords")
alertPopUpVC.modalPresentationStyle = .overCurrentContext
alertPopUpVC.modalTransitionStyle = .crossDissolve
viewController?.present(alertPopUpVC, animated: true, completion: nil)
}
雖然在代碼的開頭,我將passwordsand初始化reviewNum為“”,但在另一個 swift 檔案中,我撰寫了代碼
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ReviewItem.self), for: indexPath) as! ReviewItem
cell.viewController = self
cell.passwords = reviews[indexPath.section].password
cell.reviewNum = reviews[indexPath.section].reviewNum
所以passwordsandreviewNum被改變了,print("Password: \(passwords)")并且print("Review_number: \(reviewNum)")成功地作業了。
所以我類似地撰寫了代碼。但是,在這種情況下(我上傳的第一個代碼),
let PopUpVC = PopUpVC()
PopUpVC.pw = self.passwords
PopUpVC.reviewDoc = self.reviewNum
值沒有改變。
右下角是部分PopUpVC
class PopUpVC: UIViewController{
var reviewDoc: String = ""
var pw: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print("PopUpVC - UIViewController called")
print(pw)
print(reviewDoc)
}
...
}
我希望有人能告訴我問題是什么以及如何解決問題。
謝謝你。
uj5u.com熱心網友回復:
您沒有正確初始化 PopUpVC。這是你應該如何做的:
@IBAction func removeReview(_ sender: Any) {
print("ReviewItem - removeReview function called")
print("Passwords: \(passwords)")
print("Review_number: \(reviewNum)")
let storyboard = UIStoryboard(name: "PopUp", bundle: nil)
let alertPopUpVC = storyboard.instantiateViewController(withIdentifier: "VerifyPasswords") as! PopUpVC // make sure this identifier is a PopUpVC class!!!
alertPopUpVC.pw = self.passwords
alertPopUpVC.reviewDoc = self.reviewNum
alertPopUpVC.modalPresentationStyle = .overCurrentContext
alertPopUpVC.modalTransitionStyle = .crossDissolve
viewController?.present(alertPopUpVC, animated: true, completion: nil)
}
您正在初始化 PopUpVC 并呈現通過 Storyboard 初始化的不同實體。
我希望這有幫助!:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/488276.html
上一篇:如何使用情節提要給出拐角半徑?
