InputAccessoryView未显示 - Swift 3 Xcode 8.3 beta(非storyBoard)

时间:2017-01-29 04:12:35

标签: ios swift xcode ios10

我有一个具有InputAccessoryView的消息视图控制器。我遇到的问题是当我从第一个视图控制器传递(用户数据)到消息控制器时,InputAccessoryView没有显示。现在,当我从消息列表控制器将(用户数据)传递给消息控制器时,InputAccessoryView显示。

这是消息控制器:

class MessageViewController: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate {    
var user: User? {
    didSet {
        navigationItem.title = user?.first_name
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.keyboardDismissMode = .interactive
    setupKeyboardObservers()
}

lazy var inputContainerView: ChatInputContainerView = {
    let chatInputContainerView = ChatInputContainerView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50))
    chatInputContainerView.newMessageViewController = self
    return chatInputContainerView
}()

override var inputAccessoryView: UIView? {
    get {
        return inputContainerView
    }
}

override var canBecomeFirstResponder : Bool {
    return true
}

func setupKeyboardObservers() {
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}

func handleKeyboardDidShow() {
    if messages.count > 0 {
        let indexPath = IndexPath(item: messages.count - 1, section: 0)
        collectionView?.scrollToItem(at: indexPath, at: .top, animated: true)
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}
}

消息列表控制器:

class MessageViewController: UITableViewController {
    let user = User()
    let newMessageViewController =  NewMessageViewController(collectionViewLayout: UICollectionViewFlowLayout())
    //Send user data to (newMessageViewController) and show  newMessageViewController view controller
    newMessageViewController.user = user
    newMessageViewController.hidesBottomBarWhenPushed = true          
    self.navigationController?.pushViewController(MessageViewController, animated: true)
}

现在,这很好用!这是问题的起点。 第一个视图控制器有一个子视图,通过delagate将(用户数据)发送到superview。

delegate?.sendBackToDrag(passBackFromBlur: user)

然后,sendBackToDrag方法呈现blurPopOver视图控制器。

func sendBackToDrag(passBackFromBlur: User) {
    let blurPopOver = BlurPopOver()
    blurPopOver.user = passBackFromBlur as User
    blurPopOver.delegate = self
    blurPopOver.modalPresentationStyle = .overFullScreen
    present(blurPopOver, animated: false, completion: nil)
}

在呈现blurPopOver之后,它会将(用户数据)发送回第一个视图控制器。

delegate?.sendBackToFind(passBackToFind: user)

当通过委托传回数据时,显示消息视图控制器。

func sendBackToFind(passBackToFind: User) {
    let newMessageViewController = NewMessageViewController(collectionViewLayout: UICollectionViewFlowLayout())
    newMessageViewController.user = passBackToFind
    newMessageViewController.hidesBottomBarWhenPushed = true
    self.navigationController?.pushViewController(MessageViewController, animated: true)
}

以下是协议:

protocol MessageDelegateDrag {
    func sendBackToDrag(passBackFromBlur: User)
}

protocol MessageDelegateFind {
    func sendBackToFind(passBackToFind: User)
}

enter image description here

现在,当我像这样传递(用户数据)时它会起作用:

    func sendBackToFind(passBackToFind: User) {
    let user = User()
    user.id = "user_id"
    user.first_name = "user_first_name"
    user.last_name = "user_last_name"
    user.profile_image = "user_image_url"

    let newMessageViewController = NewMessageViewController(collectionViewLayout: UICollectionViewFlowLayout())
    newMessageViewController.user = user
    newMessageViewController.hidesBottomBarWhenPushed = true
    self.navigationController?.pushViewController(MessageViewController, animated: true)
}

似乎通过在sendBackToFind方法中将数据作为常量添加工作正常。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我最终将子视图添加到第一个视图控制器并从子视图中推送消息控制器。完全删除弹出控制器。我不确定这是否是最佳解决方案,但它确实有效!