0

so I have this lazy var for a UIView in my messaging app. The View holds a textfield, and 2 buttons. We'll call it inputView. inputView has been created programmatically and whenever I present it via: present(chatLogController, animated: true) It works as expected. here is an image to show you:

enter image description here

However, whenever I place ChatLogVC inside of a container view and present it through an embedded segue with Interface Builder the view never gets displayed. Here is a picture

enter image description here

NOTE: The reason I am using the containerView is to have the header bar at the top.

Why would an embedded segue prevent the variable to be executed?

Here's my code

Presenting VC:

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


        **MARK: Performs the function below
        self.showChatControllerForUser(user)

        **MARK:  Performs segue to VC which then performs the embed segue
        self.performSegue(withIdentifier: "toChatLog", sender: nil)

}

 **MARK: This function shows successfully the inputView
func showChatControllerForUser(_ user: messageUser) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.user = user
    present(chatLogController, animated: true)
    //navigationController?.pushViewController(chatLogController, animated: true)
}

ChatLogVC :

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.collectionViewLayout = UICollectionViewFlowLayout()
    user = segueUser
    observeMessages()
    print("CHAT LOG IS RUNNING")
    collectionView?.showsVerticalScrollIndicator = false
    collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
    //        collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
    collectionView?.alwaysBounceVertical = true
    collectionView?.backgroundColor = UIColor.white
    collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)

    collectionView?.keyboardDismissMode = .interactive
    //(inputContainerView)
    collectionView?.bringSubview(toFront: inputContainerView)



}



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

InputView:

I have left out all of the UI Code

class ChatInputContainerView: UIView, UITextFieldDelegate {

 weak var chatLogController: ChatLogController? {
    didSet {
        sendButton.addTarget(chatLogController, action: #selector(ChatLogController.handleSend), for: .touchUpInside)
        uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: chatLogController, action: #selector(ChatLogController.handleUploadTap)))
    }
}
 func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    chatLogController?.handleSend()
    return true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

My question is: Why doesn't the inputView get executed when the ChatLog is presented through a segue??

ethanfox27
  • 890
  • 1
  • 9
  • 25
  • Thi sis very confusing... when you select a table row, you are calling a function which calls `present(chatLogController, animated: true)` (so it **presents** the view controller), and then you are ***also*** calling `self.performSegue(withIdentifier: "toChatLog", sender: nil)`??? But then... your images make it look like you're using a Navigation Controller? – DonMag Aug 09 '17 at 18:19
  • Sorry I'll change it. I have them there because I was experimenting with both. And I am not using a navigation controller. – ethanfox27 Aug 09 '17 at 18:21
  • for future reference: **Minimal, Complete, and Verifiable Example** https://stackoverflow.com/help/mcve – DonMag Aug 09 '17 at 18:24
  • @DonMag cleaned up the code – ethanfox27 Aug 09 '17 at 18:29
  • @DonMag I turn one off then use the other and vice versa... The problem is it doe not get exectuted when the segue is performed only when I use "present" – ethanfox27 Aug 09 '17 at 19:39
  • `The reason I am using the containerView is to have the header bar at the top` why not simply use a navigation controller? You can hide and show the navigation controller as you want from you respective view controllers, this way you don't need to use a Container. This is not a correct usage of a container view . A container view is used as a "reusable entity" throughout your application, showing a "Top bar" is not the right reason to use one. But if you are content at using a container view then you will have to override the `prepareForSegue` method inside the parent view controller. – Anjan Biswas Aug 09 '17 at 22:16
  • Check this answer on how to override `prepareForSegue` for embedded container views - https://stackoverflow.com/questions/17763457/how-do-i-pass-data-from-my-viewcontroller-to-a-container-view – Anjan Biswas Aug 09 '17 at 22:17
  • I'm not having trouble passing objects... I am trying to figure out why my input view doesnt show up when its parent is presented through a segue! – ethanfox27 Aug 10 '17 at 12:47
  • @Annjawn dfghgfjghjrtewe – ethanfox27 Aug 10 '17 at 13:07
  • @ethanfox27 it's unclear what you are asking but just an FYI, a container view will fire a segue to load the target view inside the container view. So the container view's segue behaves like any other storyboard segue. – Anjan Biswas Aug 10 '17 at 15:44
  • I am asking why doesn't the lazy var load when I perform a segue vs. when I present it does. that's the question. what's the input view? Check the pic. @Annjawn – ethanfox27 Aug 10 '17 at 15:46

0 Answers0