0

I have a XIB file with a custom cell, which has two buttons.

Basically, I want a dialogue box of some sort to pop up when the user taps the buttons which will inform them of details. I tried to display an alert view from the corresponding Swift file but as the XIB file inherits from a UITableViewCell I cannot present the alert controller. I also want the user to be able to edit the information displayed if possible (via alert controller).

In this context I want the button to display the user's Instagram and Twitter @usernames.

import UIKit

class SocialsTableViewCell: UITableViewCell {
    @IBOutlet var instagramButton: UIButton!
    @IBOutlet var twitterButton: UIButton!
    
    var instagramAt = ""
    var twitterAt = ""
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    
    @IBAction func instagramTapped(_ sender: UIButton) {
        print("iNSTA TAPPED")
    }
    
    @IBAction func twitterTapped(_ sender: UIButton) {
        print(twitterAt)
    }
}
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
  • you want an alert or a pop up? They are not the same thing, yes you cant present an alert from cell it is inherited from UIView you need a viewController/subclass of UIViewController instance to present an alert or perform any kind of screen navigation typically – Sandeep Bhandari Feb 17 '21 at 15:43
  • yeah i just stated that i tired to present an alert, and came to the conclusion i cant. Yes i want to present a pop up from the xib file. – Francis Adewale Feb 17 '21 at 16:01
  • I think the answer posted below already answers your question. If you still need help with how to present a pop up you can follow up here – Sandeep Bhandari Feb 17 '21 at 16:20

1 Answers1

0

You can not present anything from the cell class. You need UIViewController to present any controller.

There are many ways to achieve this.

Way 1: Present your controller on the root controller. Like this

@IBAction func twitterTapped(_ sender: UIButton) {
    UIApplication.shared.windows.first?.rootViewController?.present(/*Your controller*/, animated: true, completion: nil) //<==Here
}

Way 2: Present your controller on the top most controller. How to find top most controller.

@IBAction func twitterTapped(_ sender: UIButton) {
    UIApplication().topMostViewController()?.present(/*Your controller*/, animated: true, completion: nil) //<==Here
}

Way 3: Bind your action directly to the view controller class and present your controller on self. For this if you need index path you can check this. How to get indexPath?

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52