【问题标题】:swift IBAction on button with two classes带有两个类的按钮上的 swift IBAction
【发布时间】:2015-12-09 21:05:27
【问题描述】:

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

我解释一下:

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

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

代码视图控制器:

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:]

我该怎么做?

【问题讨论】:

    标签: ios swift button uiviewcontroller unrecognized-selector


    【解决方案1】:

    您可以修改您的 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)
    

    【讨论】:

    • 谢谢你,效果很好!!只是一个问题,这是一个好方法还是从一开始我就错了?但你的解决方案对我有用
    • 我不能告诉你这是不是一个好方法,因为我也是一个新手。我只能告诉你如何解决你的问题。所以如果它有效,为什么不使用它呢?也许其他人可以告诉你什么更好)。如果可以,请不要忘记接受它作为答案)。
    • 谢谢你,编码愉快!)
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多