gpt4 book ai didi

ios - ViewController 对象为 nil

转载 作者:可可西里 更新时间:2023-11-01 01:16:51 25 4
gpt4 key购买 nike

我有 2 个 View Controller 。
1. View Controller (VC)
2. WebView Controller (WVC)

在 VC 中,我单击一个按钮并显示 WVC。在成功完成 WVC 中的所有任务后,我想显示 VC 并在 VC 中执行特定功能。

现在,Storyboard 包含一些对象并通过单击和拖动添加到 VC。最初,它有一些值(不是零)。我单击按钮,推送 WVC,完成 WVC 中的所有任务后,VC被推送。

现在VC中有些值是nil。错误,欢迎 (UILabel, UIView) 为零...而且也是想在VC中调用一个函数。怎么可能?

代码

ViewController.swift

class LoginController:UIViewController,UITextFieldDelegate{
@IBOutlet weak var password: UITextField!
@IBOutlet weak var username: UITextField!
@IBOutlet weak var error: UILabel!
@IBOutlet weak var welocome: UILabel!

......

@IBAction func BtnClicked(sender: AnyObject) {
let webViewCnt = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as UIViewController
self.navigationController?.pushViewController(webViewCnt, animated: true)
}
func doSomeProcess(){

}
}

WebViewController.swift

class WebViewController: UIViewController, UITextFieldDelegate,UIWebViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self

self.start()
}
func start(){
---- do some process ----
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let ViewCont = self.storyboard!.instantiateViewControllerWithIdentifier("LoginController") as UIViewController

self.navigationController?.pushViewController(ViewCont, animated: true)
ViewController().doSomeProcess() // call viewcontroller doSomeProcess() function
}
})
}
}

最佳答案

很简单,您正在将 VC 推向 WVC,所以在将 WVC 推送到 NavigationController 之后合乎逻辑。它将 WVC 添加到导航堆栈的顶部。最好从导航中弹出此 WVC 将自动显示具有先前值的 VC。

使用这个

ViewController.swift

class LoginController:UIViewController,UITextFieldDelegate{
@IBOutlet weak var password: UITextField!
@IBOutlet weak var username: UITextField!
@IBOutlet weak var error: UILabel!
@IBOutlet weak var welocome: UILabel!
var isBackFromWVC = false
......

@IBAction func BtnClicked(sender: AnyObject) {
let webViewCnt = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as UIViewController
self.navigationController?.pushViewController(webViewCnt, animated: true)
isBackFromWVC = true
}
func doSomeProcess(){

}
func vieWillAppear(){
if(isBackFromWVC){
doSomeProcess()
}
}
}

WebViewController.swift

class WebViewController: UIViewController, UITextFieldDelegate,UIWebViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self

self.start()
}
func start(){
---- do some process ----
dispatch_async(dispatch_get_main_queue(), { () -> Void in

self.navigationController?.popViewController(animated:true)
}
})
}
}

关于ios - ViewController 对象为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com