I am creating a UIButton in another file opposed to the main ViewController. I created this button
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
newNoteButton.setTitle("New Note", forState: UIControlState.Normal)
newNoteButton.userInteractionEnabled = true
self.view.addSubview(newNoteButton)
with action
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
It is throwing the above error even though, if i copy and paste the same code into my ViewController, it doesn't flag me at all. Why is it doing this? It is meant to just print out the string "New Note" but something causes Thread 1 to queue.
Edit: After a bit of reading, I have been able to reduce the amount of code to just this:
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
and this:
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
I tried removing the action and the problem was still there. This exists within my textView class in a separate file. In my AppDelegate file, the Root View Controller is ViewController. If I move the button to ViewController, the issue does not present itself. The only code in my ViewController is this
import Foundation
import UIKit
class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
//Global Variables
var scrollView = UIScrollView()
override func viewDidLoad(){
super.viewDidLoad()
//Add textView
//let textViewRef = textView() Removed to test
//self.view.addSubview(textViewRef.view)
//Button Code
var newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newNoteButton)
}
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
}
I have tried messing about with conditionals and the semicolon in the action but that doesn't seem to affect it or help in any way. If you need any more information, please feel free to ask.
Edit 2
My other View Controller looks like this:
import Foundation
import UIKit
class textView: UIViewController {
//Globals
var newNoteButton = UIButton()
override func viewDidLoad()
newNoteButton = UIButton(frame: CGRectMake(5, 18, 152.5, 37))
newNoteButton.backgroundColor = UIColor.grayColor()
newNoteButton.addTarget(self, action: ("newNoteButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(newNoteButton)
}
func newNoteButtonAction (sender: UIButton!){
println("New Note")
}
}
Edit 3: I just noticed that this information might be a bit relevant. The error is on the class AppDelegate
line in my delegate file.