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.