timer.invalidate() causes a crash,saying thread1:breakpoint1.1 - ios

import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var secondsLabel: UILabel!
#IBOutlet weak var milliSecondsLabel: UILabel!
var milliTimer=NSTimer()
var count:Int=1
var milliCount:Int=1
#IBAction func StartPauseTimer(sender: AnyObject)
{
milliTimer=NSTimer.scheduledTimerWithTimeInterval(1/100, target: self, selector: Selector("milliIncTimer"), userInfo: nil, repeats: true)
}
#IBAction func StopTimer(sender: AnyObject)
{
milliTimer.invalidate()
count = 1 ///this the place i get the error
//on pressing stop button,
//it says thread1:breakpoint1.1
milliCount = 1
secondsLabel.text = "0"
milliSecondsLabel.text = " "
}
override func viewDidLoad()
{
super.viewDidLoad()
}
func milliIncTimer()
{
if milliCount==101
{
milliCount = 1
secondsLabel.text = "\(count++)"
}
milliSecondsLabel.text = "\(milliCount++)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the logs say this
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
(lldb)

You may have placed a breakpoint accidently.
In your code on the line:
count = 1
Now on that line look left and you will see a blue arrow.
You can delete the arrow by dragging this arrow to the right and let go.
Hope this helped you!

Related

today extension shows "unable to load" after button event (iOS)

Good morning!
I have an "unable to load" problem in my iOS widget. I've read a lot of about the "unable to load" message but nothing fixed my problem. I'm not sure but I think my problem is to refresh the widget after changing my content.
My widget has one button and one label. If the user press the button the text from the label will changed - in this moment the widget shows "unable to load". Just a milisecond after pressing the button.
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var segment_att: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.NewData)
}
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
#IBAction func button_ae(sender: AnyObject) {
let tableviewclass = TodayTableViewController()
tableviewclass.newData()
}
}
Important is that the label is shown in a TableViewCell of a TableViewController. So the TableViewController is embeded in the ViewController within a Container... The listener from the button call the method newdata() of the file of the TableViewController.
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
let meldung: String = "test"
label1.text = meldung
}
func newData() {
let meldung: String = "new test"
label1.text = meldung
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The code is really simple and basic - so I'm wondering about the problem in this simple mechanism. I hope you can help me!
Thanks at all!
Your code assumes that label1 has been set when newData() is called, even immediately after the constructor is called.
Try using this optional chaining syntax instead, which will quietly fail if the property is nil:
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
var meldung: String = "test" // <-- meldung is property
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
label1?.text = melding // <-- optional chaining
}
func newData() {
melding = "new test" // <-- set the class property
label1?.text = meldung // <-- optional chaining
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and instead of calling newData(), you might instead just set the meldung property, e.g.:
tableviewclass.meldung = "new test"
as your viewDidLoad() will take care of setting the UILabel text from the property

call array items with a delay using in swift - get UIResponder error

Searching around didn't seem to provide anything that I understood straight away.
I'm trying to make a stupid 'old skool' loading screen for an app which will print out 'DOS like' statements.
My current code looks like this, but I get a UIResponder, UIApplicationDelegate error - EXE_BAD_ACCESS
I've tried copying this to a new project etc. still get the same error.
Code below:
class ViewController: UIViewController {
#IBOutlet weak var loadingLBL: UILabel!
#IBOutlet weak var logoIMG: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//fades logo image in
logoIMG.alpha = 0
UIView.animateWithDuration(5, animations: {
self.logoIMG.alpha = 1;
})
//array for loading text
var loading = ["Loading please wait.......\n", "Registry , OK!\n", `"Available ram, 256k\n", "BIOS Load... OK\n", "Welcome" ]`
var i = 0
var str: String = ""
var timer = NSTimer()
//delay function for loading array text
func delayFunc() {
str += "\(loading[i])\n"
loadingLBL.text = str
if i == loading.count - 1 {
timer.invalidate()
}
i += 1
}
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector(delayFunc()), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
As I mentioned, I get a crash and I've no idea what I've done wrong.
Any help would be much appreciated.
I think your problem is that delayFunc() is in viewDidLoad, the selector is looking in the viewController but can't find the function (because it is in viewDidLoad)
Edit: I wasn't 100% clear about why the selector can't find delayFunc when it's in viewDidLoad. If you declare a function within another function's body it's lifetime is at most the lifetime of its 'parent' function. So when your timer tries to call delayFunc, delayFunc doesn't exist anymore because viewDidLoad has finished running.
Try:
var i = 0
var str: String = ""
var timer = NSTimer()
var loading = ["Loading please wait.......\n", "Registry , OK!\n", "Available ram, 256k\n", "BIOS Load... OK\n", "Welcome" ]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//fades logo image in
logoIMG.alpha = 0
UIView.animateWithDuration(5, animations: {
self.logoIMG.alpha = 1;
})
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "delayFunc", userInfo: nil, repeats: true)
}
func delayFunc() {
str += "\(loading[i])\n"
loadingLBL.text = str
if i == loading.count - 1 {
timer.invalidate()
}
i += 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

SIGABRT error in Xcode 7

I'm having trouble compiling this code in Swift successfully. Its a program for stopwatch. Two labels(start and stop) and a textfield as the output. It says its a delegate problem and highlights this in red:
class AppDelegate: UIResponder, UIApplicationDelegate
My code for the project is as follows:
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var counter = 1
#IBOutlet weak var Label: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateCounter()
{
counter += 1
Label.text = String(counter)
}
#IBAction func Start(sender: AnyObject)
{
counter = 0
Label.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector ("updateCounter"), userInfo: nil, repeats: true)
}
#IBAction func Stop(sender: AnyObject)
{
timer.invalidate()
}
}
Very common problem, here's what you have to do:
Go through every outlet on your storyboard and delete it.
Reconnect every function in your ViewController to the outlets.
The problem, I believe, is that you have changed the name of an outlet in your code, and when the storyboard looks for that outlet it crashes your app. Make sure that when you rename your outlets, you reconnect it too!

Making a basic tally counter in iOS

I am having difficulty in making simple tally counter application. I have put the codes but when i hit "run simulation", it always gives error message. can anyone help me find my mistake? thank you.
my codes are like below:
import UIKit
class ViewController: UIViewController {
var counter: Int!
#IBOutlet weak var Display: UILabel!
func updateDisplay() {
Display.text = String(counter)
}
override func viewDidLoad() {
super.viewDidLoad()
counter = 0
updateDisplay()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func PlusButton(sender: AnyObject) {
counter = counter + 1
updateDisplay()
}
#IBAction func MinusButton(sender: AnyObject) {
counter = counter - 1
updateDisplay()
}
#IBAction func ClearButton(sender: AnyObject) {
counter = 0
updateDisplay()
}
}
here is the appearance in the Xcode
https://www.facebook.com/photo.php?fbid=10204747519271053&set=pcb.10204747520111074&type=1&theater
here is the error message when i hit run simulation
https://www.facebook.com/photo.php?fbid=10204747519351055&set=pcb.10204747520111074&type=1&theater
Your code is working fine but I think you have problem with layOut connection so check it again and it should be like this:
For Display label:
For PlusButton :
For MinusButton:
And For ClearButton:
Check THIS sample project for more Info.

How do you get a UI button to do different tasks each time it is pressed?

When I create an IBaction for a button I can easily get it to perform an action for example:
#IBAction func click(sender: AnyObject) {
label6.hidden = false}
How do I get it to execute a totally different task when it is clicked for a second or third time etc?
Thanks
EDIT: When I try this I get an error "Viewcontroller.type does not have a member named "label1", this also is stated for "label2" Any ideas why as I have already added the label in as an outlet. Do I need to declare it somewhere else to get it to work? Thanks
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
label1.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
class MyButtonClass
{
var clickCounter: Int = 0
#IBAction func click(sender: AnyObject)
{
clickCounter += 1
if (clickCounter == 1)
{label1.text = "text1"}
else if (clickCounter == 2)
{label2.text = "text1"
clickCounter = 0}
}
}
}
Replace your code with the following:
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
label1.text = ""
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
var clickCounter: Int = 0
#IBAction func click(sender: AnyObject)
{
clickCounter += 1
if (clickCounter == 1)
{
label1.text = "text1"
}
else if (clickCounter == 2)
{
label2.text = "text1"
clickCounter = 0
}
}
}
You can have a counter and depending on its value fire another method from the one called on click
Create a property that counts the tap count
Increment the tap count on each click
Implement a switch on the tap count to perform different actions (IBAction acts as a dispatcher)

Resources