How invoke a method in the method addTarget of the

2019-08-07 04:35发布

I have a problem with the method "addTarget". In my program I read a file containing info on the Graphical Interface. According to this file, I programatically create the graphical elements.

I have a class for any possible type of element. For example, I have the Button class and a Text class. When I read and parse the file, I create an object of the appropriate class. If, for example, I read the tag "Button" I create a Button object, and set its properties (width, height, text, colour and gravity) and then I insert this object in a tree.

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

}

After I read the file, I print the tree in order. To print the tree, I call the method "draw" of my object. The method "draw" receives the UIViewController to print its different elements. Up to this point everything works all right.

The problem is when I click the button. I need to write the method in the principal UIViewController. My doubt is if there is any way to write this method in my Button class and then call it.

标签: ios oop swift
2条回答
干净又极端
2楼-- · 2019-08-07 04:54

You could add the following code in your Button class initializer:

    button.addTarget(self, action: Selector("yourMethod"), forControlEvents: .TouchUpInside)

yourMethod must be implemented in your Button class

查看更多
姐就是有狂的资本
3楼-- · 2019-08-07 05:00

I found a solution to solve my problem. I did that the "button" class inherit of "UIButton" class. This forced me to override the "init" method and others, but, to solve this other problem, I used a "convenience init". Thus, I can have a "constructor" without override the principal constructors of UIButton. In the other hand, I changed the code to create a button in the "draw" method. I put the new values in attributes that my class inherit of the father instead of create a new button.

// 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)
    }
}
查看更多
登录 后发表回答