Ollie G Ollie G - 1 month ago 8
Swift Question

UIButton Call in separate Class

I am using an expandable tableView. One of the cells will be used for a button to open a URL link in safari. The button IBOulet is in WebsiteCell class and the data and view controller are in Detail View.

In the Detail View class I have an error while trying to call the button where it says

websiteCell.venueURL(UIButton)
. The error says 'Extra Argument in call'.

What should be here instead of UIButton?

WebsiteCell Class

class WebsiteCell: UITableViewCell {



@IBAction func venueURL(sender: UIButton) {


}


Detail View Class

else if indexPath.row == 3 {
let websiteCell = tableView.dequeueReusableCellWithIdentifier("websiteCell") as! WebsiteCell

let venueURL = venues!["URL"] as! String



websiteCell.venueURL(UIButton){
UIApplication.sharedApplication().openURL(NSURL(string: venueURL)!)



}

Answer

I would add the button in storyboard to a prototype cell of type websiteCell. Then you can link this button to your websiteCell class as an @IBAction, and the code to open the URL should be placed in the IBAction.

class WebsiteCell: UITableViewCell {

    var venueURL: String?

    @IBAction func venueURL(sender: UIButton) {
        UIApplication.sharedApplication().openURL(NSURL(string: venueURL)!)
    }

}

Then, your cellForRowAtIndexPath method will look like this:

else if indexPath.row == 3 {

    let websiteCell = tableView.dequeueReusableCellWithIdentifier("websiteCell") as! WebsiteCell

    websiteCell.venueURL = //url here

    return websiteCell

}
Comments