繁体   English   中英

带有两个类的按钮上的快速IBAction

[英]swift IBAction on button with two classes

我想知道是否可以通过在另一个类中创建的按钮来触发动作。

我解释:

我有两个类,一个是视图控制器,另一个是用于创建视图的类。

在视图控制器中,我调用第二个类中的方法来创建自定义视图。 然后,将自定义视图添加到主视图(请参见下面的代码)。 自定义视图显示一个按钮,我不知道如何使用按钮,因为在运行应用程序时找不到作为目标创建的方法。

代码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();
    }

}

第二类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();
    }
}

我希望我的按钮创建customView调用方法cancelView当我按下它,但我不设法去做。

这是我得到的错误:

NSForwarding:警告:类'Myproject.CustomView'的对象0x523c8没有实现methodSignatureForSelector:-提前发生问题无法识别的选择器+ [Myproject.CustomView cancelView:]

我该怎么做?

您可以修改createPopupViewWithList以接受UIViewController的1个参数,然后将按钮的目标设置为其。

码:

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
}

然后从FirstViewController调用函数:

popupViewBeforeOrderCoupon = CustomView.createPopupViewWithList(self)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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