[英]how to enable the customview controller with view after dismiss Second viewcontroller in swift
我有兩個視圖控制器imag 1.第一個視圖控制器是主要的,第二個是CustomAlertviw控制器主視圖控制器就像上面的圖像,當我點擊繼續顯示customalertview控制器,就像圖像一樣。 當點擊輸入pin按鈕時我必須關閉視圖控制器並在主視圖控制器上顯示帶有uiview的視圖控制器但是嘗試了,它顯示了視圖控制器,還有一個自定義視圖沒有顯示它崩潰我的應用程序
主視圖代碼
@IBAction func backButtonClicked(_ sender: UIButton) {
let myAlert = UIStoryboard(name: "CustomAlertview", bundle: nil).instantiateViewController(withIdentifier: "CustomAlertViewController") as? CustomAlertViewController
myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(myAlert!, animated: true, completion: nil)
}
Customalertview controller
@IBAction func enterPinButtonClick(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
let myAlert = UIStoryboard(name: "ScanAndPay", bundle: nil).instantiateViewController(withIdentifier: "ScanAndPayDetailsEntryViewController") as? ScanAndPayDetailsEntryViewController
myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(myAlert!, animated: true, completion: nil)
myAlert.valuespass()
}
inside main view controller one function calling from the customalrerview
func valuespass()
{
DispatchQueue.main.async {
self.authType = 1
self.authStatus = false
print("this is calling")
self.pinStatusLabel.text = "Please enter a 4-digit Security PIN"
}
當我從自定義警報視圖應用程序調用此函數時崩潰並顯示hread 1:致命錯誤:在解開Optional值時意外發現nil此錯誤。 如何在解除自定義視圖控制器后顯示所有信息。
你差不多完成了。 從FirstView
呈現ScanAndPay
而不是CustomAlertview
。
我已經完成了使用Delegate
因為我認為它適合這種情況。 按照以下步驟 -
第1步:
在FirstViewController中 ,
添加 - CustomAlertviewDelegate
這樣的事情 -
class FirstViewController: UIViewController, CustomAlertviewDelegate {
現在替換你的backButtonClicked
方法 -
@IBAction func backButtonClicked(_ sender: UIButton) {
let myAlert = UIStoryboard(name: "CustomAlertview", bundle: nil).instantiateViewController(withIdentifier: "CustomAlertViewController") as? CustomAlertViewController
myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
myAlert.delegate = self
self.present(myAlert!, animated: true, completion: nil)
}
添加CustomAlertviewDelegate
的新Delegate
方法 -
func ScanAndPayView(){
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
let myAlert = UIStoryboard(name: "ScanAndPay", bundle: nil).instantiateViewController(withIdentifier: "ScanAndPayDetailsEntryViewController") as? ScanAndPayDetailsEntryViewController
myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(myAlert!, animated: true, completion: nil)
}
}
第2步:
現在是CustomAlertview的時間,
為類的頂級添加協議 -
protocol CustomAlertviewDelegate: class {
func ScanAndPayView()
}
class CustomAlertview: UIViewController{
weak var delegate : CustomAlertviewDelegate!
...
override func viewDidLoad() { // Rest of your code
}
現在是時候關閉當前視圖控制器並通知FirstViewController
呈現ScanAndPay view Controller
-
@IBAction func enterPinButtonClick(_ sender: Any) {
delegate.ScanAndPayView()
self.dismiss(animated: true, completion: nil)
}
希望它能幫助你實現你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.