Can't push to another view controller

Hi guys, I have a view controller. Within this view controller, I have a subview named SearchUserSubView containing a table view.

Since I am not able to push to another view controller within this view, I tried to do following thing:

class SearchUserSubView:UIView {
...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let userCell = tableView.cellForRow(at: indexPath) as! SearchUserTableViewCell
        let userObject = userResultArray[indexPath.row]
        
        // if the profile image is not loaded, don't allow clicking on the cell
        if(userCell.profileImageView.image == nil) {
            return
        }
        
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let searchContentController = storyBoard.instantiateViewController(withIdentifier: "searchContent") as! SearchContentViewController
        searchContentController.showProfileViewController(userObject: userObject, userProfileImage: userCell.profileImageView.image!)
    }

I am trying to call the showProfileViewController method, defined in my view controller class. And it is actually called:

func showProfileViewController(userObject: User, userProfileImage: UIImage) {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let selectedUserController = storyBoard.instantiateViewController(withIdentifier: "selectedUserProfile") as! SelectedUserProfileViewController
        selectedUserController.userObject = userObject
        selectedUserController.userProfileImage = userProfileImage
        let navController = storyBoard.instantiateViewController(withIdentifier: "searchContentNav") as! UINavigationController
        navController.pushViewController(selectedUserController, animated: true)
    }

Whatever I do here, nothing is pushed towards the SelectedUserProfileViewController.

Can anyone tell me, why this is the case?

Thanks for clarifying. But, you may need to clarify more about your view controller hierarchy.


I assumed you have something like this:


[UINavigationContoller]

|

[SearchContentViewController]


But from the fact you can observe `self.navigationController` is nil, my assumption was wrong.


Please try to illustrate the hierarchy of your view controllers.

You write STORYBOARD and not storyboard.


Is it on purpose ?

What you assumed is correct. My SearchContentViewController is embedded in a navigation controller when I have a look at the storyboard.

Yes, since I use this code part on different places. I introduced a global constant named 'STORYBOARD'.

let STORYBOARD : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

You instantiate many view controllers without using segue, and that makes your app's runtime structure completely different than that shown in the storyboard. If your `SearchContentViewController` is really embedded in a navigation controller at runtime, `self.navigationController` cannot be nil.

I know, that's the reason why it is so strange. Could this be related to the fact that when I call the showProfileViewController(..)

method, that the SearchContentViewController is not fully loaded somehow? I mean, I call the method by using a delegate so the viewDidLoad methods as an example is not called.

Something strange may be happening, or you may be missing a simple fact that your `SearchContentViewController` is not embedded in a navigation controller.


Anyway, you are showing very limited part of code and we cannot find which.


As for now, I bet on the latter.


You should better show how you present `SearchContentViewController` with enough info we can confirm it's embeded in a navigation controller. Storyboard something cannot be any confirmation.

Sir, I don't know why. But out of sudden, it works now. I really can't explain why. Once I find it out, I will let you know 🙂

That sort of thing may happen when you are struggling with codes. Not necessarily, but understanding what was happening can be a good practice for me.


By the way, you would not call your colleague as sir, yes? The same here.

Can't push to another view controller
 
 
Q