当我以编程方式在Swift中的普通类中创建按钮时,如何调用Button类的方法addTarget中的方法

时间:2014-09-08 09:34:54

标签: ios oop swift

我的方法有问题" addTarget"。在我的程序中,我读了一个包含图形界面信息的文件。根据这个文件,我以编程方式创建了图形元素。

我有一个类可用于任何可能类型的元素。例如,我有Button类和Text类。当我读取并解析文件时,我创建了相应类的对象。例如,如果我读取了标签" Button"我创建一个Button对象,并设置其属性(宽度,高度,文本,颜色和重力),然后将该对象插入树中。

// ViewController.swift

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {
    private let defaultPage : NSString = "http://xxx/index.xml"
    private var dataArray : NSArray = []     

    internal override func viewDidLoad() {
        super.viewDidLoad()
        var parser : XMLParser = XMLParser.alloc().initWithURL(NSURL.URLWithString(defaultPage)) as XMLParser
        parser.getViews().draw(self)
    }    

    func buttonAction(sender: UIButton!) {
        NSLog("Method1")
   }
}

// XMLParser
import Foundation
class XMLParser: NSObject, NSXMLParserDelegate {

    private var views: Views?
    private var view: View?
    private var menu: Menu?
    private var menuItem: MenuItem?
    private var button: Button?
    private var parser = NSXMLParser()
    private var elements = NSMutableDictionary()
    private var element = NSString()

    internal func initWithURL(url :NSURL) -> AnyObject {
        beginParsing(url)
        return self
    }    

    private func beginParsing(xmlUrl :NSURL) {
        parser = NSXMLParser(contentsOfURL: xmlUrl)
        parser.shouldProcessNamespaces = false
        parser.shouldReportNamespacePrefixes = false
        parser.shouldResolveExternalEntities = false
        parser.delegate = self
        parser.parse()
    }

    internal func getViews() -> View {
        return view!
    }

    internal func parser(parser: NSXMLParser!,didStartElement elementName: String!, namespaceURI: String!, qualifiedName : String!, attributes attributeDict: NSDictionary!) {
        element = elementName.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        switch(element as NSString){
        /*...*/
        case "button":
                button = Button(id:"", link:attributeDict.objectForKey("link") as NSString, width:attributeDict.objectForKey("width") as NSString, gravity:attributeDict.objectForKey("gravity") as NSString, text: "")
                view!.appendButton(button!)
                break
            default:
                break
        }

    }
}

// ButtonClass
class Button{
    private var id: String
    private var link: String
    private var width: String
    private var gravity: String
    private var text: String    

    internal init(id: String, link: String, width: String, gravity: String, text: String){
        self.id = id
        self.link = link
        self.width = width
        self.gravity = gravity
        self.text = text
    }

    internal func draw(uiViewController: UIViewController, position: Int){

        /*Different operations ...*/
        var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(x!, 130+50*CGFloat(position), CGFloat(width!), 40)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle(self.text, forState: UIControlState.Normal)
        //button.addTarget(uiViewController, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        button.addTarget(self, action: Selector("buttonAction2"), forControlEvents: .TouchUpInside)
        uiViewController.view.addSubview(button)
    }

    func buttonAction2() {
        NSLog("h2")
    }

}

读完文件后,我按顺序打印树。要打印树,我打电话给方法"画"我的目标。方法"画"接收UIViewController以打印其不同的元素。到目前为止,一切正常。

问题是当我点击按钮时。我需要在主体UIViewController中编写该方法。我怀疑是否有任何方法可以在我的Button类中编写此方法然后调用它。

2 个答案:

答案 0 :(得分:0)

您可以在Button类初始值设定项中添加以下代码:

    button.addTarget(self, action: Selector("yourMethod"), forControlEvents: .TouchUpInside)
必须在Button类

中实现

yourMethod

答案 1 :(得分:0)

我找到了解决问题的解决方案。我做了那个"按钮"继承了" UIButton"类。这迫使我覆盖" init"方法和其他方法,但是,为了解决这个问题,我使用了"方便初始化"。因此,我可以有一个"构造函数"不覆盖UIButton的主要构造函数。另一方面,我更改了代码以在" draw"中创建一个按钮。方法。我将新值放在我的类继承父类的属性中,而不是创建一个新按钮。

// ButtonClass
import Foundation
import UIKit

class Button: UIButton{

    private var idButton: String = ""
    private var link: String = ""
    private var width: String = ""
    private var gravity: String = ""
    private var text: String = ""

    convenience init(idButton: String, link: String, width: String, gravity: String, text: String){
        self.init()
        self.idButton = idButton
        self.link = link
        self.width = width
        self.gravity = gravity
        self.text = text
    }

    internal func draw(uiViewController: UIViewController, position: Int){
        /*Different operations ...*/
        self.frame = CGRectMake(x!, 130+50*CGFloat(position), CGFloat(width!), 40)
        self.backgroundColor = UIColor.greenColor()
        self.setTitle(self.text, forState: UIControlState.Normal)
        self.addTarget(self, action: "buttonActionTouchUpInside:", forControlEvents: .TouchUpInside)
        uiViewController.view.addSubview(self)
    }

    internal func buttonActionTouchUpInside(sender: UIButton!) {
        NSLog("h2: "+self.text)
    }
}