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!
Related
I've recently been working on making a Stopwatch app that is similar to the one on the stock iOS devices. I have the timer working but I would like it to follow the same look and exact time like the stopwatch Apple has. As of right now I have it just showing the seconds. For what I know there has to be some kind of to convert the numbers to the 00:00.00 format.
import UIKit
class ViewController: UIViewController {
var counter = 0
var timer = Timer()
#IBOutlet weak var timerLbl: UILabel!
#IBAction func startBtn( sender: Any) {
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
}
#IBAction func stopBtn( sender: Any) {
timer.invalidate()
counter = 0
}
func updateTimer() {
counter += 1
timerLbl.text = "(counter)"
}
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.
}
}
You should create a reference date when you create the timer, and then every time the timer fires, get the current time by creating a date, and then get the time between the two dates using a calendar. You can get more information about how to do that here
Start of app (Main storyboard but it's fine in simulator to.)
After clicking play button in mutates into something that isn't a 1,2,3....
Here is the code in the viewcontroller and it's not much. Pretty simple stopwatch app.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
var counter = 0
var clock : Timer = Timer()
override func viewDidLoad() {
timeLabel.text = String(counter)
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.
}
#IBAction func playButton(_ sender: Any) {
clock = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer(){
timeLabel.text = String(describing: counter += 1)
}
#IBAction func pauseButton(_ sender: Any) {
clock.invalidate()
}
#IBAction func resetButton(_ sender: Any) {
clock.invalidate()
counter = 0
timeLabel.text = String(counter)
}
}
I've tried setting constraints but that just shoots out "ambiguous" warnings all over the place. Any suggestions? Ideas?
The problem is at your updateTimer method. You need to move your variable increment to the line above and then create a string representation from the variable:
func updateTimer() {
counter += 1
timeLabel.text = String(counter)
}
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.
}
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.
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!