Search code examples
iosswiftbuttonuiviewcontrollerunrecognized-selector

swift IBAction on button with two classes


I wonder if I can trigger an action with a button created in an another class.

I explain:

I have two classes, the view controller, and a class used to create a view.

In the view controller, I call a method located in the second class to create a custom view. Then I add the custom view to the main view (see code below). The custom view displays a button and I don't know how I can use my button because the method created as a target is not found when I run my app.

Code viewController:

import UIKit

class FirstViewController: UIViewController {

    var popupViewBeforeOrderCoupon:UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        popupViewBeforeOrderCoupon = CustomView.createPopupViewWithList()
        self.view.addSubview(popupViewBeforeOrderCoupon!)        
    }

    func cancelView(sender: UIButton!) {
        var alertView = UIAlertView();
        alertView.addButtonWithTitle("OK");
        alertView.title = "Alert";
        alertView.message = "Button Pressed!!!";
        alertView.show();
    }

}

and the second class CustomView:

import Foundation
import UIKit

class CustomView {

    init () {

    }

    static func createPopupViewWithList() -> UIView? {
        var dynamicView = UIView(frame: CGRectMake(100, 200, 200, 100))
        dynamicView.backgroundColor = UIColor.grayColor()
        dynamicView.alpha = 1
        dynamicView.layer.cornerRadius = 5
        dynamicView.layer.borderWidth = 2

        let button = UIButton();
        button.setTitle("Add", forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        button.frame = CGRectMake(10, 10, 100, 50)
        dynamicView.addSubview(button)

        button.addTarget(self, action: "cancelView:", forControlEvents: .TouchUpInside)

        return dynamicView
    }

    func cancelView(sender: UIButton!) {
        var alertView = UIAlertView();
        alertView.addButtonWithTitle("OK");
        alertView.title = "Alert";
        alertView.message = "Button Pressed!!!";
        alertView.show();
    }
}

I want my button created in customView to call the method cancelView when I press it but I don't manage to do it.

Here's the error I get :

NSForwarding: warning: object 0x523c8 of class 'Myproject.CustomView' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector +[Myproject.CustomView cancelView:]

How can I do it?


Solution

  • You can modify your createPopupViewWithList to accept 1 argument of UIViewController and then set button's target to it.

    Code:

    static func createPopupViewWithList(controller: UIViewController) -> UIView? {
        var dynamicView = UIView(frame: CGRectMake(100, 200, 200, 100))
        dynamicView.backgroundColor = UIColor.grayColor()
        dynamicView.alpha = 1
        dynamicView.layer.cornerRadius = 5
        dynamicView.layer.borderWidth = 2
    
        let button = UIButton();
        button.setTitle("Add", forState: .Normal)
        button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        button.frame = CGRectMake(10, 10, 100, 50)
        dynamicView.addSubview(button)
    
        // set your controller here
        button.addTarget(controller, action: "cancelView:", forControlEvents: .TouchUpInside)
    
        return dynamicView
    }
    

    Then call your function from FirstViewController:

    popupViewBeforeOrderCoupon = CustomView.createPopupViewWithList(self)