gpt4 book ai didi

swift - 使用标识符初始化 ViewController 后隐藏的导航栏

转载 作者:行者123 更新时间:2023-11-28 15:28:14 25 4
gpt4 key购买 nike

我正在对我的应用进行聊天,但我仍然遇到这个问题。如您所见,我有一个 ChatListTableViewController,其中包含所有事件聊天的列表(图 1,左上 View )。当我单击一个单元格时,我成功打开了 ChatViewController,其中 NavBar 显示了与我聊天的人的姓名、后退箭头和一个用于删除聊天的垃圾按钮。但是,在 ChatListTableViewController 中,我有一个“+”按钮(图 1,左上角),它会打开一个 PopUpViewController:这个按钮允许您与从 TableView 中选择的用户开始新的聊天。从图 1 中可以看出,问题是当我单击 PopUpChatListViewController 中的单元格时,我尝试初始化 ChatViewController 并打开它,但隐藏了 NavigationBar。这是通过使用 TableView 的“didSelectRowAt”函数完成的.我将 PopUpChatListViewController 的代码发布给您。在 ChatViewController 的 viewDidLoad 中,我尝试设置“navigationBar.isHidden = false”,但什么也没有。你能解释一下这里发生了什么吗?

import UIKit

class PopUpChatListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return userName.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellPopUp", for: indexPath)
cell.textLabel?.text = userName[indexPath.row]
return cell
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UserDefaults.standard.set(userID[indexPath.row], forKey: "chatID")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatActivity") as! ChatViewController
self.present(vc, animated: true, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

运行应用程序的屏幕截图 View :

enter image description here

主要 Storyboard方案:

enter image description here

最佳答案

如果您想使用push segue,请查看@Leo Valentim 的回答。如果您仍想使用 modal segue,我的回答是给您的。

更改此代码:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UserDefaults.standard.set(userID[indexPath.row], forKey: "chatID")

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatActivity") as! ChatViewController

self.present(vc, animated: true, completion: nil)
}

收件人:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UserDefaults.standard.set(userID[indexPath.row], forKey: "chatID")

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatActivity") as! ChatViewController
let navigationController = UINavigationController()
navigationController.viewControllers = [vc]

self.present(navigationController, animated: true, completion: nil)
}

可以引用这个问题:Modal segue, navigation bar disappears

这里:Using Segues (注意Table 9-1 Adaptive segue types,它包括segue类型和每种类型的行为)

关于swift - 使用标识符初始化 ViewController 后隐藏的导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44956078/

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