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.
Related
I'm currently developing a counter app which every time you press a button it will count up. I have already done this and works 100%, what I'm trying to do is make a another button and when you press it, it shows the current number on the console but it only prints out 0 because that's the default variable I assigned the value to.
My whole class:
var counterNumber = 0
#IBOutlet weak var counterLabel: UILabel!
func initCount(){
counterNumber = 0
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
#IBAction func CountUp(sender: UIButton) {
numberUp()
}
#IBAction func RestartButton(sender: UIButton) {
initCount()
}
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
override func viewDidLoad() {
super.viewDidLoad()
initCount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Just call method numberUp in printButton: action.
var counterNumber = 0//this is a variable of class
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
Use 'self' keyword to call instance variables.
class ViewController: UIViewController {
// Properties
var counterNumber:Int = 0 // Make this variable Global to Class
// IBOutlets Properties
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp()
self.counterLabel.text = "\(self.counterNumber)"
print("\n You Pressed ==> \(sender.title) button \(self.counterNumber) times")
}
func numberUp() {
self.counterNumber += 1
self.counterLabel.text = "\(counterNumber)"
}
}
I want Subtract process with Button action but my code does not work.
I made the Number. This number is Main number for user's game money. I set the '1,000'. This I set the name of this "playerMoney"
After that I made the #IBAction Button for Discount the '500' number from 'playerMoney'. This button's action means buy Game item things.
I set the name of '500' number is "chocolatePrice" and Button's name is "buyChocolateButton"
I also print playerMoney on the UILable.
I set that label name is "printPlayerMoney"
I'm using the code below.
import UIKit
class ViewController: UIViewController {
var playerMoney = 1000
var chocolatePrice = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
}
override func viewDidLoad() {
super.viewDidLoad()
printPlayerMoney.text = "\(playerMoney)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You need to put the printPlayerMoney.text = "\(playerMoney)" inside the button function. That way, overtime you press the button, it does the calculation and then updates the label. See the updated code below:
class ViewController: UIViewController {
var playerMoney: Int = 1000
var chocolatePrice: Int = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
printPlayerMoney.text = "\(playerMoney)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
printPlayerMoney.text = "\(playerMoney)"
doesn't watch the value of playerMoney
After changing the value of playerMoney, we have to set the text of the UILabel again if we're expecting a visual change
So quick question. I am writing an iOS app for a dart game. And I am trying to implement an undo button to undo the last action.
Whats going on: When you push any of the buttons they add that amount to the score. The users score that the buttons have an effect on is the currentPlayer.
I want the undo button to undo whatever button was pushed last. I have been researching and it seems like they are all showing how to hard code an undo action. However it could be any of the 6 buttons hit_single, hit_double, attack_double... etc.
FIRST: I don't know how to use undoManager.registerUndoWithTarget(target:AnyObject, selector: Selector, object: AnyObject?), I'm not sure how to apply this and what each of these 3 things do.
SECOND: In my game you have to go from 1-20(the numbers on a dart board) and then hit bullseye to win. The variables user1score and user2score increase their count to display what number the player is aiming at. However, after 20 I want the score to display "Bullseye". I am not sure if there is a way to write a mixed content array, one that allows counting 1-20, and then the 20th array value say "Bullseye". Or maybe write an If statement that says if the user1score/user2score is greater than 20 print "Bullseye". The problem is it says its an integer and I cant input a string value.
Here is my code:
import UIKit
class Galaxy_VC: UIViewController {
#IBOutlet var currentPlayerName: UILabel!
#IBOutlet weak var user1score: UILabel!
#IBOutlet weak var user2score: UILabel!
#IBOutlet weak var darts_Thrown: UILabel!
var names = ["Big Meat", "J Hooks"]
var currentPlayer = 0
var scores = [0, 0]
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
setupGame()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupGame() {
user1score.text = "\(count)"
user2score.text = "\(count)"
}
func updateTurn() {
currentPlayerName.text = names[currentPlayer]
user1score.text = "\(scores[0])"
user2score.text = "\(scores[1])"
}
func undoLast(score: NSNumber) {
undoManager?.registerUndoWithTarget(self, handler: <#T##AnyObject -> ()#>)
}
#IBAction func btn_NextTurn(sender: UIButton) {
currentPlayer = 1 - currentPlayer
updateTurn()
}
#IBAction func btn_UndoTurn(sender: AnyObject) {
}
#IBAction func hit_Single(sender: AnyObject) {
scores[currentPlayer] + 1
updateTurn()
}
#IBAction func hit_Double(sender: AnyObject) {
scores[currentPlayer] + 2
updateTurn()
}
#IBAction func hit_Triple(sender: AnyObject) {
scores[currentPlayer] + 3
updateTurn()
}
#IBAction func Miss(sender: AnyObject) {
scores[currentPlayer] - 1
updateTurn()
}
#IBAction func attack_double(sender: AnyObject) {
scores[1 - currentPlayer] - 2
updateTurn()
}
#IBAction func attack_Triple(sender: AnyObject) {
scores[1 - currentPlayer] - 3
updateTurn()
}
}
Any help you can provide would be really appreciated! Thanks!
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)
I can't seem to write the if statement in the end to make the outs go up when the strikes hit 3?
can someone lease help and make it so in swift the outs will go up when someone hits 3 strikes
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
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.
}
#IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
#IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
#IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
#IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}
Use a property observer:
var counter = 1 {
didSet {
if counter == 3 {
self.outsCounter++
}
}
}
Whenever counter gets changed, didSet will be called.
(Also note that the equality operator is ==. = is for assignment.)