0

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.

elito25
  • 624
  • 1
  • 6
  • 14
  • everything looks fine ... take a look at this http://stackoverflow.com/questions/24102191/make-a-uibutton-programatically-in-swift – Sam B Jun 19 '14 at 02:02
  • @SamBudda I complied with that. It runs perfectly within my ViewController but not within my other class. – elito25 Jun 19 '14 at 02:04
  • 2
    What is your other class if not a view controller? What is `self.view`? – Aaron Brager Jun 19 '14 at 02:05
  • It shouldn't matter where you create this button. Most likely button is not your problem – Sam B Jun 19 '14 at 02:05
  • My other class is a view controller, but the one that I mentioned in the Delegate was my primary ViewController. Then, I have another view controller that I have named textView where. @AaronBrager – elito25 Jun 19 '14 at 02:07
  • This error is thrown when I press the button so my best guess is that it is caused by the button. It builds fine but as soon as the button is pressed, it sends me that error. @SamBudda – elito25 Jun 19 '14 at 02:08
  • You code instructs the button to call the newNoteButtonAction method of self; does self respond to that selector? – Jesse Rusak Jun 19 '14 at 02:12
  • It should be. I have the button code within the viewDidLoad() and the action outside the viewDidLoad but within the textView class. I don't see why I am getting so many down votes. It is a valid question which I am having problems with. @JesseRusak – elito25 Jun 19 '14 at 02:15
  • @elito25 You're getting downvotes because there isn't nearly enough information in your question to help you, and because you haven't tried to isolate the problem. For example, does it still happen if you remove the action? What backtrace are you getting? Have you looked at http://stackoverflow.com/help/mcve ? – Jesse Rusak Jun 19 '14 at 02:18
  • @JesseRusak Sorry, still getting used to Stack Overflow. I will try to rewrite my question to comply with the link you provided. – elito25 Jun 19 '14 at 02:21
  • @JesseRusak I believe I have added enough information. I toyed about a bit more with the code but I am still lost. – elito25 Jun 19 '14 at 02:35
  • 1
    @elito25 You posted the code for the view controller that's working… can you post the code for the view controller that's *not* working? And describe how it is presented on the view? The problem is likely with your view controller, not your button. – Aaron Brager Jun 19 '14 at 12:58
  • @AaronBrager I added the code for my other view controller, textView. When I am testing with the button in textView, I remove the button in ViewController and uncomment the textViewRef and add it to the View. – elito25 Jun 19 '14 at 20:33
  • @AaronBrager I don't mean to be rude but I am still stuck on this question. – elito25 Jun 20 '14 at 01:48

2 Answers2

0

I had the same problem and I received the same error that you. I wanted create a button in other class. I recommend you read my solution: How invoke a method in the method addTarget of the Button class when I create programmatically the button in a plain class in Swift . An abstract, you should inherit of the "UIClass" that you require. If you want to create a button, you should inherit of "UIButton" class. Then, you set the values of the attributes of this class instead of create a button.

Community
  • 1
  • 1
CGG
  • 253
  • 1
  • 11
  • 22
0

FYI for anybody with this issue in the future, it can usually be debugged back to the retention of the button within the class you are adding the target to.

Try moving the declaration of the button or targeted class up to a class global to be cleaned up after the view is disposed of.

Example: In the instance listed above, declare the newNoteButton at the class level instead of inside the viewDidLoad function to ensure it's retained while it can call it's target.

class ViewController: UIViewController, UITextViewDelegate, UIScrollViewDelegate {

    // Global Variables
    var scrollView = UIScrollView()
    var newNoteButton: UIButton! // <- Declare button here instead of in viewDidLoad

}
AgentK
  • 665
  • 7
  • 8