简体   繁体   中英

MFMessageComposeViewController in a UITableView (Swift)

I'm trying to show an MFMessageComposeViewController from within a custom TableViewCell class but I get an error saying: "Multiple inheritance from classes UITableViewCell and UIViewController." I know that UIViewController is required for MFMessageComposeViewController, so how can I go about fixing this?

My code:

class TableViewCell: UITableViewCell, MFMessageComposeViewControllerDelegate {

  var userNumber: String!

 @IBAction func callButton(sender: AnyObject) {

    UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(userNumber)")!)

}

@IBAction func textButton(sender: AnyObject) {

    let message = MFMessageComposeViewController()
    message.body = ""
    message.recipients = ["\(userNumber)"]
    message.messageComposeDelegate = self


}

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {


}

You indeed need a UIViewController to present a MFMessageComposeViewController . To solve your problem, you can add a property to your TableViewCell :

var viewController: UIViewController?

You need to assign the current view controller in the cellForRowAtIndexPath function - if the view controller is the data source of the table view, this would be just cell.viewController = self .

Then, in the textButton function, you can present the message composer:

self.viewController.presentViewController(message, true, nil)

For the dismissal in didFinishWithResult , just call

self.viewController.dismissViewController(true, nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM