快速关闭viewController并选择到下一个控制器

时间:2018-08-20 02:25:33

标签: swift uinavigationcontroller segue

我有一个带有导航栏的tableviewcontroller。当我单击表格单元格时,将带我进入具有表格视图的导航,这是一个人的列表。当我单击某个人时,应该弹出该视图以将我带到第一个视图,但随后立即选择下一个视图控制器,该控制器将是消息传递表控制器。

这是第一个控制器。

class MessagingViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

       setupNavBarWithUser()

    }

func setupNavBarWithUser(_ user: User) {
    if let profileImageUrl = user.profileImageUrl {
        profileNavImage.loadImageUsingCacheWithUrlString(urlString: profileImageUrl)
    }

    nameLabel.text = user.name        
    let recognizer = UITapGestureRecognizer(target: self, action: #selector(MessagingViewController.showChatController))
    containerView.addGestureRecognizer(recognizer)
    containerView.isUserInteractionEnabled = true
}

//when user taps on there name at nav bar
@objc func showChatController() {        

    print("Show Chat Log")
    performSegue(withIdentifier: "GoToChatLog", sender: nil)

}

这里是人员列表的控制者。

var messagesController: MessagingViewController?


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

            self.messagesController?.showChatController()

            let userSelected = self.users[indexPath.row]

            print("User selected: \(indexPath.row)")
            print(userSelected.name!)
          navigationController?.popViewController(animated: true)

}

我认为这是我可以做的...任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

一种方法是使用委托模式。因此,您的第二个控制器将类似于以下内容。

First we create a delegate something like

protocol UserSelectDelegate: class {
    func didSelectUser(user: User) //this will be called when you select the user
}

now implement the delegate on your second controller

class UserListController: UIViewController {

    weak var userDelegate: UserSelectDelegate?

    //method to call from tableView cell didSelect method
    func triggerDelegate(with user: User) {
        navigationController?.popViewController(animated: true)
        guard let delegate = userDelegate else { return }
        delegate.didSelectUser(user: user)
    }

完成后,您应该遵循firstController的委托

class MessagingViewController: UIViewController, SeconViewControllerDelegate {


    var userListController: UserListController!

    func showSecondController() {
        userListController = UserListController()  //initialize second controller that will be pushed when tablecell is tapped
        userListController.userDelegate = self
        self.navigationController?.pushViewController(userListController, animated: true)
}


    func didSelectUser(user: User) {
        //perform you segue here

        //goto third controller
    }
}

这将完成您打算做的事情,但是由于由于用动画弹出viewcontroller会导致延迟,因此您也需要考虑到这一点。因此,您在MessageController上的委托实现看起来像这样

func didSelectUser(user: User) {

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            //perform segue to third controller
        }


    }
上面的

现在应该可以按您预期的那样工作,但是解决方案有点笨拙。因此,我建议的是

Instead of pushing Viewcontroller you should present it so that in second controller we dismiss it, and the dismiss method will give you completion handler in which you should trigger the delegate