enter image description here enter image description here import UIKit import Foundation

class ViewController: UIViewController {

var myButton = UIButton() var startTimer_a: NSTimer! // ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ запуск var startTimer_b: NSTimer! // ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Ρ‹ ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΠΎΠ² override func viewDidLoad() { super.viewDidLoad() func puskA() { startTimer_a = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "runMeA",userInfo:nil,repeats: false) //ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄ изобраТСния ΠΈΠ· массива startTimer_b = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "runMeA",userInfo:nil,repeats: true) //ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Ρ‹ ΠΏΠΎΠ²Ρ‚ΠΎΡ€Π° Π²Ρ‹Π²ΠΎΠ΄Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ ΠΈΠ· массива } // скобка func puskA puskA() } // скобка закрытия viewDidLoad func runMeA() { /******************** Π±Π»ΠΎΠΊ button off ****************************/ // Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΊΠ°Π΄Ρ€Π° ΠΈ Π΅Π³ΠΎ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ постоянны self.myButton.frame = CGRectMake(265, 597, 120, 40) // Π±Π»ΠΎΠΊ button = off self.myButton.setTitle("off ", forState: UIControlState.Normal) self.myButton.addTarget(self, action:"runMeG", forControlEvents: UIControlEvents.TouchUpInside) self.myButton.showsTouchWhenHighlighted = true // Π²ΡΠΏΡ‹ΡˆΠΊΠ° ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ self.myButton.setTitleColor(UIColor.redColor(), forState: .Normal) //.Highlighted...red Ρ†Π²Π΅Ρ‚ надписи self.myButton.layer.cornerRadius = 8 // радиус ΡƒΠ³Π»ΠΎΠ² ΠΊΠ½ΠΎΠΏΠΊΠΈ self.myButton.backgroundColor = UIColor.cyanColor() // Π·Π°Π»ΠΈΠ²ΠΊΠ° ΠΊΠ½ΠΎΠΏΠΊΠΈ self.myButton.layer.borderColor = UIColor.redColor().CGColor self.myButton.titleLabel?.font = UIFont(name: "Helvetica-Bold", size: 18) self.view.addSubview(self.myButton) self.view.bringSubviewToFront(myButton) self.myButton.userInteractionEnabled = true; // true - false } // скобка закрытия func runMeAdd /********** Π±Π»ΠΎΠΊ button On *******************/ func runMeG() { // запуск ΠΎΡ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ button = off startTimer_a.invalidate() // Π²Ρ‹ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠ² запуска... startTimer_b.invalidate() self.myButton.frame = CGRectMake(265, 597, 120, 40) // Π±Π»ΠΎΠΊ button надпись = On self.myButton.setTitle("On", forState: UIControlState.Normal) self.myButton.addTarget(self, action: "puskA", forControlEvents: UIControlEvents.TouchUpInside) // ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ button ΠΊΠΎΠ³Π΄Π° надпись = "On" Ρ‡Π΅Ρ€Π΅Π· action: "puskA" Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ снова Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠ², //ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π±Π°Ρ‚ΠΎΠ½Π΅ action "puskA" Π½Π΅ выполняСтся с Π²Ρ‹Π΄Π°Ρ‡Π΅ΠΉ ΠΏΠΎ AppDelegate.swift сообщСния // Thread 1: signal SIGABRT = ? //self.myButton.setTitleColor(UIColor.redColor(), forState: .Normal) // эти ΠΊΠΎΠ΄Ρ‹ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ ΠΈΠ»ΠΈ //self.myButton.layer.borderColor = UIColor.blackColor().CGColor // ΠΌΠΎΠΆΠ½ΠΎ Π·Π°ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ // значСния... //self.myButton.titleLabel?.font = UIFont(name: "Helvetica-Bold", size: 18) //self.view.addSubview(self.myButton) //self.view.bringSubviewToFront(myButton) // ΠΊΠΎΠ½Π΅Ρ† Π±Π»ΠΎΠΊΠ° button = off adds // startTimer0014.fire() Π½Π΅ срабатываСт... } // скобка закрытия runMeG } // скобка закрытия class ViewController: UIViewController 

// works fine, except for attempts to restart the timer block by pressing the button when the text = "On"
// tried parameters:
// func puskA (firstName: NSTimer = startTimer_a, secondName: NSTimer = startTimer_b)
// puskA (startTimer_a, secondName: startTimer_b) - starting from button ON
// before launching Xcode, the OS does not issue ...
// when run with parameters, when you click a loaf with the caption = ON, it gives OS = Thread1 BAD INSTRACTION
// codes are compiled Xcode 7.2 ON

[enter image description here] [13]


import UIKit import Foundation

class ViewController: UIViewController {

  var myButton = UIButton() override func viewDidLoad() { super.viewDidLoad() } // скобка закрытия viewDidLoad var startTimer_a: NSTimer! // ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ запуск var startTimer_b: NSTimer! // ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Ρ‹ ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΠΎΠ² func puskA() { startTimer_a = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "runMeA",userInfo:nil,repeats: false) //ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄ изобраТСния ΠΈΠ· массива startTimer_b = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "runMeA",userInfo:nil,repeats: true) //ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Ρ‹ ΠΏΠΎΠ²Ρ‚ΠΎΡ€Π° Π²Ρ‹Π²ΠΎΠ΄Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ ΠΈΠ· массива } // скобка func puskA puskA() // Xcode Ρ‚Ρ€Π΅Π±ΡƒΠ΅Ρ‚ Expection declaration func runMeA() { /******************** Π±Π»ΠΎΠΊ button off ****************************/ // Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΊΠ°Π΄Ρ€Π° ΠΈ Π΅Π³ΠΎ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ постоянны self.myButton.frame = CGRectMake(265, 597, 120, 40) // Π±Π»ΠΎΠΊ button = off self.myButton.setTitle("off ", forState: UIControlState.Normal) self.myButton.addTarget(self, action:"runMeG", forControlEvents: UIControlEvents.TouchUpInside) self.myButton.showsTouchWhenHighlighted = true // Π²ΡΠΏΡ‹ΡˆΠΊΠ° ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ self.myButton.setTitleColor(UIColor.redColor(), forState: .Normal) //.Highlighted...red Ρ†Π²Π΅Ρ‚ надписи self.myButton.layer.cornerRadius = 8 // радиус ΡƒΠ³Π»ΠΎΠ² ΠΊΠ½ΠΎΠΏΠΊΠΈ self.myButton.backgroundColor = UIColor.cyanColor() // Π·Π°Π»ΠΈΠ²ΠΊΠ° ΠΊΠ½ΠΎΠΏΠΊΠΈ self.myButton.layer.borderColor = UIColor.redColor().CGColor self.myButton.titleLabel?.font = UIFont(name: "Helvetica-Bold", size: 18) self.view.addSubview(self.myButton) self.view.bringSubviewToFront(myButton) self.myButton.userInteractionEnabled = true; // true - false } // скобка закрытия func runMeAdd /********** Π±Π»ΠΎΠΊ button On *******************/ func runMeG() { // запуск ΠΎΡ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ button = off startTimer_a.invalidate() // Π²Ρ‹ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠ² запуска... startTimer_b.invalidate() self.myButton.frame = CGRectMake(265, 597, 120, 40) // Π±Π»ΠΎΠΊ button надпись = On self.myButton.setTitle("On", forState: UIControlState.Normal) self.myButton.addTarget(self, action: "puskA", forControlEvents: UIControlEvents.TouchUpInside) self.myButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents) // ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ button ΠΊΠΎΠ³Π΄Π° надпись = "On" Ρ‡Π΅Ρ€Π΅Π· action: "puskA" Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ снова Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠ², //ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π±Π°Ρ‚ΠΎΠ½Π΅ action "puskA" Π½Π΅ выполняСтся с Π²Ρ‹Π΄Π°Ρ‡Π΅ΠΉ ΠΏΠΎ AppDelegate.swift сообщСния // Thread 1: signal SIGABRT = ? //self.myButton.setTitleColor(UIColor.redColor(), forState: .Normal) // эти ΠΊΠΎΠ΄Ρ‹ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ ΠΈΠ»ΠΈ //self.myButton.layer.borderColor = UIColor.blackColor().CGColor // ΠΌΠΎΠΆΠ½ΠΎ Π·Π°ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ // значСния... //self.myButton.titleLabel?.font = UIFont(name: "Helvetica-Bold", size: 18) //self.view.addSubview(self.myButton) //self.view.bringSubviewToFront(myButton) // ΠΊΠΎΠ½Π΅Ρ† Π±Π»ΠΎΠΊΠ° button = off adds // startTimer0014.fire() Π½Π΅ срабатываСт... } // скобка закрытия runMeG } // скобка закрытия class ViewController: UIViewController 
  • Before adding a target, you need to remove the old one - markov
  • how is it and for what =? - user198638
  • button.removeTarget(nil, action: nil, forControlEvents: .AllEvents) In order for the action button.removeTarget(nil, action: nil, forControlEvents: .AllEvents) the button not to accumulate, so it turns out several times the methods will start to twitch. More without a stack errors can not tell - markov
  • how to scratch the OS = stack? - user198638 1:58 pm
  • button.removeTarget (nil, action: nil, forControlEvents: .AllEvents) tried ... does not want ... OSH is old ... - user198638

1 answer 1

puskA () is declared inside viewDidLoad and does not exist outside of it. Therefore, the error. bring it to class level to correct

  • well, the selector in swift3 is declared differently myButton.addTarget(self, action: Selector("puskA"), for: .touchUpInside) - Max Mikheyenko
  • before swift3 is still far away ... render puskA () or all together func puskA () =? I tried everything in the class body before, but it requires an Expected Declaration ... I tried to substitute parameters, but Xcode does not agree ... @ Max Mikheyenko - user198638
  • to declare the entire function in the class but not in another method - Max Mikheyenko
  • drew as done now ... after the line with stars ... @ Max Mikheyenko - user198638
  • The puskA call itself should be in viewDidLoad - Max Mikheyenko