NavigationController does not push function call + fast

I am basically trying to recreate whatsapp. My MainVC has a TableView that shows all current conversations and has a push sega in ChatVC to display the entire conversation.

When a user wants to start a new conversation, click the button in the upper right corner, and UserListVC will appear at the bottom and display a list of users.

My thinking here is that when the user clicks on the user, "UsersListVC" rejects (shows mainVC) and calls the function to open ChatVC. However, I could not successfully call the function after this error from MainVC.

enter image description here

Please, not right now, I do not click any data, I just want the segues to work correctly

MainVC.swift

func showChatVC() {

    print("FUnction called")
    let showChat = ChatVC()
    navigationController?.pushViewController(showChat, animated: false)

}

UsersListVC.swift

       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            dismiss(animated: true) { 
                MainVC().showChatVC()
            }
        }

, MainVC. ​​ , , ? UserListVC ChatVC

+4
6

, , , .

MainVC UsersListVC, ChatVC, ChatVC.

, :

  • , , , , MainVC.

    protocol UsersListDelegate: class {
    
        func newConversationCreated(withUser user: AnyObject)
    
    }
    

  1. (MainVC)

    class MainVC: UIViewController, UsersListDelegate {
    
        func newConversationCreated(withUser user: AnyObject) {
    
            //I'll fill this in later in my answer
    
        }
    
    }
    

    1. UsersListVC,

      class UsersListVC: UIViewController {
      
          weak var delegate: UsersListDelegate!
      
      }
      

      1. UsersList MainVC, MainVC (self), , UsersList, segue.

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
            if let destination = segue.destination as? UsersListVC {
        
                destination.delegate = self
        
            }
        
        }
        

        segue:

        func presentUsersList() {
        
            let userListsVC = UsersListVC()
            userListsVC.delegate = self
            self.present(userListsVC, animated: true)
        
        }
        

. , UserListsVC, :

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        delegate.newConversationCreated(withUser: /*Your User Object*/)

    }

MainVC newConversationCreated :

  • ChatVC
  • ChatVC
  • ChatVC

.

    func newConversationCreated(withUser user: AnyObject) {

        dismiss(animated: true) { 

            let showChat = ChatVC()
            showChat.user = user
            navigationController?.pushViewController(showChat, animated: false)

        }

    }

p.s. MainVC, (. question ). UserLists, .

+1

- 1

  • .

  • MainVC.swift

     func showChatVC() {
    
      let showChat = self.storyboard!.instantiateViewControllerWithIdentifier("ChatVc_ID") as! ChatVC
      self.navigationController?.pushViewController(showChat, animated: false)    
    
     }
    

UsersListVC.swift

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    dismiss(animated: true) { 
   let mainVC = self.storyboard!.instantiateViewControllerWithIdentifier("MainVC_ID") as! MainVC 
   mainVC.showChatVC()
         }
}
+1

tableView didSelectRow,

UIApplication.sharedApplication().sendAction("showChatVC", to: nil, from: nil, forEvent: nil)

, .

0

NotificationCenter:

MainVC NewChatCreateNotification :

NotificationCenter.default.addObserver(self, selector: #selector(autoNavigateToNewChat(_:)), name: NSNotification.Name(rawValue: "NewChatCreateNotification"), object: nil)

MainVC, autoNavigateToNewChat: ChatVC

UsersListVC

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Use this dict to populate the userInfo of the notification, then you can use it when you push to ChatVC. 
    //You can obviously add more info to it like userId etc. as long as the data conforms to: [AnyHashable: Any]
    let dict = ["chatId" as NSString : <the selected chatId> as NSNumber]

    dismiss(animated: true) { 
        NotificationCenter.default.post(name: Notification.Name(rawValue: "NewChatCreatedNotification"), object: self, userInfo: dict as [AnyHashable: Any])
    }
}
0

. segue . UsersListVC.

import Foundation
import UIKit

internal enum SegueIdentifier: String {
    case showChat = "showChat"
    case showUserList = "showUserList"
}

internal final class MainVC: UIViewController {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == SegueIdentifier.showUserList.rawValue, let vc = segue.destination as? UsersListVC {
            vc.completion = {
                [weak self] in self?.performSegue(withIdentifier: SegueIdentifier.showChat.rawValue, sender: nil)
            }
        }
    }
}

internal final class ChatVC: UIViewController {

}

internal final class UsersListVC: UIViewController {

    internal var completion: (() -> ())? = nil

    @IBAction func close(_ sender: UIButton) {
        dismiss(animated: true) {
            [weak self] in self?.completion?()
        }
    }
}
0

, MainVC .

dismiss(animated: true) { 
    MainVC().showChatVC()
}

ViewController , ViewController.

MainVC.

dismiss(animated: true) { [weak self] in
    if let mainVC = self?.presentingViewController as? MainVC {
        self?.mainVC.showChatVC()
    }
}

"".

0

Source: https://habr.com/ru/post/1657443/


All Articles