Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Creating a button that calls a method in another class1

I have 2 classes, the first class is named "store" where I create a button which calls a method: "storeSelected" located in the second class with the name ExploreViewController.
The method should print "done" and take me to another view controller. Without the segue "done" is printed but when putting the segue code the app crashes.

The error is:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<Glam.ExploreViewController: 0x14ed3be20>) has no segue with identifier 'ok'' .........

// ExploreViewController Class

let _sharedMonitor: ExploreViewController = { ExploreViewController() }()

class ExploreViewController: UIViewController, UIScrollViewDelegate {

    class func sharedMonitor() -> ExploreViewController {
        return _sharedMonitor
    }


    func storeSelected(sender: UIButton) {                        
        println("done") // it entered here and "done" is printed                                                                   
        self.performSegueWithIdentifier("ok", sender: self) //here is the problem                           
    }
}







// another class named "Store"  
// button is created

let monitor = ExploreViewController.sharedMonitor()
btn.addTarget(monitor, action: Selector("storeSelected:"), forControlEvents: UIControlEvents.TouchUpInside)
like image 400
Omar Kayali Avatar asked Apr 13 '15 10:04

Omar Kayali


1 Answers

Assuming that you have a view controller where you create the Store object - you should pass the button action back to this view controller and add a segue from it to your desired destination.

Best practice is to use a protocol that delegates the buttons action back up to the viewController that it is contained in as below.

Store.swift

protocol StoreDelegate: NSObject {
    func didPressButton(button:UIButton)
}

class Store: UIView {

    weak var delegate:StoreDelegate!

    override init(frame:CGRect) {
        super.init(frame:frame)

        var button = UIButton()
        button.setTitle("button", forState: .Normal)
        button.addTarget(self, action: "buttonPress:", forControlEvents: .TouchUpInside)
        self.addSubview(button)
    }

    func buttonPress(button:UIButton) {
        delegate.didPressButton(button)
    }

}

ViewController.swift

class ViewController: UIViewController, StoreDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        addStoreObj()
    }

    func addStoreObj() {
        var store = Store()
        store.delegate = self // IMPORTANT
        self.view.addSubview(store)
    }

    func didPressButton(button:UIButton) {
        self.performSegueWithIdentifier("ok", sender: nil)
    }

}

This code is untested, but I hope you get the idea - your Store object delegates the button press activity back to its containing ViewController and then the ViewController carries out the segue that you have attached to it in the Storyboard.

like image 186
Wez Avatar answered Sep 24 '22 10:09

Wez