gpt4 book ai didi

ios - 为什么不同swift类中的函数没有被调用?

转载 作者:可可西里 更新时间:2023-11-01 02:02:10 26 4
gpt4 key购买 nike

新的只是由于某种原因没有得到正在发生的事情,没有错误或任何上下打印语句都被执行,而下一个类函数中的打印没有被执行???

class LoginController: UIViewController{

var blankController:ViewController?


func handleLogin(){
if let loginVCobj = self.containerCustom.currentViewController as? LoginClass
{
guard let loginEmail = loginVCobj.LoginName.text else{
print("No Login Name to submit")
return
}
guard let loginPassword = loginVCobj.LoginPassword.text else {
print("No Login Password to submit")
return
}

Auth.auth().signIn(withEmail: loginEmail, password: loginPassword){ (user, error) in
if error != nil{
print(error)
return
}
print("user Logged in") <---- its printed

///// updated to initialize ////
self.blankController = UIStoryboard(name: "Main",
bundle:nil).instantiateViewController(withIdentifier:
"SWBlankRevealVCStoryBoard") as? ViewController


self.blankController?.customNavigationBar() *-----NO PRINT
print("Passed the BlankController") . <---- its printed
self.dismiss(animated: true, completion: nil)

}

}

}
}

现在有一个名为 ViewController 的不同类

class ViewController: UITableViewController{

func customNavigationBar(){
//something about the customizing the Nav Bar

print("stupid call")
}

}

enter image description here

最佳答案

self.blankController?.customNavigationBar() *-----NO PRINT 在这一行中你的 self.blankController 可能是 nil,因为你还没有初始化你的 View Controller 。

为确保您的 View Controller 已初始化或未初始化,在使用它(其属性)之前请尝试以下操作

(将您的打印行 self.blankController?.customNavigationBar() 替换为以下内容):

print("self.blankController = \(self.blankController)")  
// it may print self.blankController = nil

if let viewcontroller = self.blankController {
viewcontroller.customNavigationBar()
} else {
print("blankController may be nil")
}

有几种方法可以初始化您的 View Controller (取决于您的 View Controller 的界面构建器文件)。

使用 Storyboard:如果您的界面构建器位于 Storyboard内

blankController = UIStoryboard(name: "<Storyboard Name/String>", bundle: nil).instantiateViewControllerWithIdentifier("<view controller identifier>") as! ViewController

// Note: Do not forget to set identifier of your view controller, otherwise this will result into app crash. You've extended your ViewController with UITableViewController, so ensure implementation of all necessary datasource and delegate in your ViewController.

这是引用图片,帮助你,如何找到/设置 View Controller 的标识符,

enter image description here


//----------------
更新
问题的原因是你的 ViewController 在你的代码中被 UITableViewController 扩展了。

class ViewController: UITableViewController

但在你的 Storyboard中,同样是属于 SWRevealViewController.... 的类,它不是 UITableViewController

解决方案

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return number of row
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
// return UITableViewCell
}
}

关于ios - 为什么不同swift类中的函数没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537869/

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