0

I want to present a pop up in the Tableview cell ViewController. I want this because in the TableViewCell is an button that needs a pop up when it is pressed. Please help me! If I run this I get the error:

InstagramClone.SignInViewController: 0x102e22b30 whose view is not in the window hierarchy!

@objc func kik (){
        if let Kik = user?.KikUsername {
            // Prepare the popup assets
            let title = "Kik Username"
            let message = "The Kik username from \(user?.username) is: \(Kik)."
            let image = UIImage(named: "kik-icon.jpg")

            // Create the dialog
            let popup = PopupDialog(title: title, message: message, image: image)

            // Create buttons
            let buttonOne = CancelButton(title: "Cancel") {
                print("You canceled")
            }

            // This button will not the dismiss the dialog
            let buttonTwo = DefaultButton(title: "Copy Kik Username to clipboard!", dismissOnTap: false) {
                UIPasteboard.general.string = Kik
            }
            popup.addButtons([buttonOne, buttonTwo])

            // Present dialog
            self.window?.rootViewController?.present(popup, animated: true, completion: nil)
        }

    }
Vine Tube
  • 15
  • 4
  • do you want to open a view controller when a tableView cell button is pressed? – Wings Apr 11 '18 at 11:47
  • `self.window?.rootViewController` that's `SignInViewController`. That's not the current ViewController that is presented, that's why you get the error. You need to tell the current viewcontroller (which hole the cell) to do it. You can use a closure or delegate pattern to do so. – Larme Apr 11 '18 at 11:56
  • @V_rohit I have a tableView Cell and in the tableView cell there is a button. I want to get the pop up when the button inside of the tableView cell is pressed! – Vine Tube Apr 11 '18 at 12:23
  • You can do this by using protocol and delegates – Wings Apr 11 '18 at 12:27
  • How? Do you have an example? – Vine Tube Apr 11 '18 at 12:33
  • Delegate: https://stackoverflow.com/a/40806707/1801544 Closure: https://stackoverflow.com/a/43474733/1801544 – Larme Apr 11 '18 at 12:42

1 Answers1

0

I think you just need to add the popup to main view:

self.view.addSubview(popup)
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
S.S.D
  • 1,579
  • 2
  • 12
  • 23