0

I'm ultimating a chat into my app but i still have this issue. As you can see i have a ChatListTableViewController with the list of all the active chats(picture 1, top-left view). When i click on a cell i successfully open che ChatViewController with the NavBar displaying the name of the person whom i chat with ,the back arrow and a trash button to delete the chat. However, in the ChatListTableViewController i have a "+" button (picture 1, top-left) which opens a PopUpViewController: this one allows you to start a new chat with a user,chosen from a TableView. The issue as you can see from picture 1 ,is that when i click on a cell in the PopUpChatListViewController, i try to initialize a ChatViewController and it opens but the NavigationBar is hidden.This is done by using the function "didSelectRowAt" of the TableView. I post you the code of the PopUpChatListViewController. In the viewDidLoad of the ChatViewController i tryied to put set "navigationBar.isHidden = false " but nothing. Can you explain me what's happening here ?

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 OF THE SCREENSHOTS RUNNING APP:

enter image description here

MAIN STORYBOARD SCHEME:

enter image description here

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • And please can you explain me how to post the code in a way that it fits the container? I always click the "{}" button and copy the code where i see ENTER YOUR CODE HERE but i always get this strange result. Thank you in advance guys! – Alexandru Popescu Jul 06 '17 at 18:08

2 Answers2

2

If you want to use push segue, check the answer of @Leo Valentim. And my answer is for you if you still want to use modal segue.

Change this code:

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)
}

To:

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)
}

You can refer to this question: Modal segue, navigation bar disappears

And here: Using Segues (Pay attention to Table 9-1 Adaptive segue types, it includes the segue types and behavior of each type)

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
  • Thank you for the answer. I followed your advice and now the navBar appears. The problem is that it doesn't have the BACK ARROW button and user is not allowed to go back to the ChatList. I'd like to avoid using the PRESENT MODALLY but is the only way i found to display this PopUpTableView. Do you suggest me another solution so that my NavBar won't disappear? – Alexandru Popescu Jul 07 '17 at 07:31
  • There are two issues we need to clarify here. **1:** What kind of segue do you want to display the `PopUpChatListViewController` from the `ChatListTableViewController` (currently is `modal segue`)?. **2:** What kind of segue do you want to display the `ChatViewController` from the `PopUpChatListViewController` (currently is `modal segue`)?. – Anh Pham Jul 07 '17 at 07:49
  • Explain more for your problem with **back button**. The **back button** is automatically added by the navigation controller to remove the topmost view controller from the stack and return to the previous screen. In our case, the `PopUpChatListViewController` perform a segue to a `UINavigationController` that has root view is `ChatViewController`. This means that if you are displaying the `ChatViewController` screen, the **back button** will not show (because in the navigation controller's stack, there is no view controller behind the `ChatViewController` at all). – Anh Pham Jul 07 '17 at 08:38
  • To solve the problem with the **back button**. In the `ChatViewController`, you can add a `UIBarButtonItem` to the navigation bar. And in the `target` (or `IBAction`, if you use `.storyboard` or `.xib` file) of that button, just add this code: `dismiss(animated: true, completion: nil)` – Anh Pham Jul 07 '17 at 08:48
  • Really good explanation. I just want to instantiate a `ChatViewController`, based on the user's choice in the `PopUpListViewController`. In particular i don't want to create a new navigationController with this ChatViewController as root because in this way i can't navigate back to the `ChatListViewControlller`, because of the issue you explained above. I think that the best choice here , is to intervene on the `PopUpListViewController` : do you have any suggestion on how to create a similar PopUpList without presenting it modally , so that after i'll be able to push the `ChatViewContr.` ? – Alexandru Popescu Jul 07 '17 at 09:07
  • I have a doubt: if i add the button that dismisses the view, programmatically or in the storyboard, it will not interfere with the NavigationBar items ? I want to explain my self better: when i click on a cell in the `ChatListViewController` , it moves to the `ChatViewController` and the NavBar displays a ***BACKBUTTON*** on the left. But if i click on a cell from the `PopUpListViewController` i instantiate a `ChatViewController` outside the previous navigation stack, and as you suggest i need to add the left button to the NavBar. – Alexandru Popescu Jul 07 '17 at 09:20
  • But if i add this button it will cover also the AUTOMATIC BACKBUTTON displayed when i pass to the `ChatViewController` from the `ChatListViewController`. No ? I hope you understood my doubt. Thank you for the patience – Alexandru Popescu Jul 07 '17 at 09:22
0

Replace

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

to

self.navigationController?.pushViewController(vc, animated: true)

When you use 'present' the view lost de navigationController.

Leo Valentim
  • 464
  • 5
  • 19
  • I do not think this code will work. The solution is correct, but in this case, `PopUpChatListViewController` does not have `UINavigationController` because it is shown by a `modal segue`. https://i.stack.imgur.com/i3o5J.png – Anh Pham Jul 06 '17 at 18:54
  • Thank you for the answer. I tryied with the pushViewController but i get the same result without NavigationBar. I thing @TrungAnhPham is right about the modal segue. But is the only way i found to display this type of PopUpTableView as you can see from picture 1 . – Alexandru Popescu Jul 07 '17 at 07:34