Swift:创建一个调用另一个class1中的方法的按钮 [英] Swift: Creating a button that calls a method in another class1

查看:901
本文介绍了Swift:创建一个调用另一个class1中的方法的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个类,第一个类名为store,我在其中创建一个调用方法的按钮:storeSelected位于第二个类中,名称为 ExploreViewController

该方法应该打印完成并带我到另一个视图控制器。
如果没有segue完成会被打印,但是在使用segue代码时应用程序会崩溃。

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.

错误是:

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'Receiver(< Glam.ExploreViewController:0x14ed3be20>)没有标识符'ok''
.. .......

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)


推荐答案

假设您有一个视图控制器,您可以在其中创建 Store 对象 - 您应该将按钮操作传回到这个视图控制器并从中添加一个segue到你想要的目的地。

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.

最佳做法是使用一个协议,将按钮操作委托给它包含的viewController,如下所示。

protocol StoreDelegate {
    func didPressButton(button:UIButton)
}

class Store: UIView {

    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



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)
    }

}

此代码未经测试,但我希望您明白这一点 - 您的商店对象将按钮按下活动委托给其包含 ViewController ,然后ViewController执行您拥有的segue在故事板中附加到它。

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.

这篇关于Swift:创建一个调用另一个class1中的方法的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆