How to implement NSUndoManager and arrays in iOS using Swift 2 - ios

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!

Related

How to use one button to add scores for different integers (swift3)

Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}

Swift - the text field returns 0 even though I inserted a different number

I'm new to Swift, and I'm trying to make a little app that, given two values (a,b) returns a random number between the two numbers (a < randomnumber < b)
The user has to write the values of a and b in two different text fields and then just press a button and that's it
The problem is:
I told the NSLog to display the two values just before generating the random number because I noticed something wasn't working as it was supposed to, and the result is that apparently the second value I insert, whether it is a or b, for some (unknown) reason, equals 0 ! For example, if I write in the a-textfield the value 10 and then I insert the value 40 in the b-textfield, the NSLog displays a = 10, b = 0. If I insert the b value before, the NSLog displays b = 40, a = 0.
These are the lines of code :
#IBAction func TextAAction(sender: AnyObject) {
a = UInt32(TextA.text!)!
}
#IBAction func TextBAction(sender: AnyObject) {
b = UInt32(TextB.text!)!
}
#IBAction func randomInBetweenAction(sender: AnyObject) {
NSLog("a=%ld, b=%ld", a, b)
randomInBetween()
unHide()
Label.text = "\(randomNumberInBetween)"
}
as you can see the value a is equal to the value that is inserted in the TextA field and the value b is equal to the value that is inserted in the TextB field
the randomInBetween() is the function that generates a random number between a and b, and it does work.. the problem is that one of the values (the second one I insert) is always 0 for some reason, even if I set it to a different number!
Any ideas on why this happens and how to work it out?
#ReinerMelian this is the full code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var Label: UILabel!
#IBOutlet weak var Button: UIButton!
#IBOutlet weak var InvariableLabel: UILabel!
#IBOutlet weak var TextA: UITextField!
#IBOutlet weak var TextB: UITextField!
#IBOutlet weak var AndLabel: UILabel!
#IBOutlet weak var Button2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var randomNumber = Int()
func randomNUMBER() -> Int {
randomNumber = Int(arc4random())
return randomNumber
}
func Hide() {
Label.hidden = true
}
func unHide() {
Label.hidden = false
}
var a = UInt32()
var b = UInt32()
var randomNumberInBetween = Int()
func randomInBetween() -> Int {
if b>a {
randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
return randomNumberInBetween
} else {
randomNumberInBetween = Int(arc4random_uniform(a - b) + a)
return randomNumberInBetween
}
}
#IBAction func ButtonAction(sender: AnyObject) {
randomNUMBER()
unHide()
Label.text = "\(randomNumber)"
NSLog("\(randomNumber)")
}
#IBAction func TextAAction(sender: AnyObject) {
a = UInt32(TextA.text!)!
}
#IBAction func TextBAction(sender: AnyObject) {
b = UInt32(TextB.text!)!
}
#IBAction func randomInBetweenAction(sender: AnyObject) {
NSLog("a=%ld, b=%ld", a, b)
randomInBetween()
NSLog("\(randomInBetween)")
unHide()
Label.text = "\(randomNumberInBetween)"
NSLog("\(randomInBetween)")
}
}
(PS: as you can see, there is also a function that generates a random number without any sort of constraints, but that part works just fine.. the problem is with the other part of the code, the one that returns a random number between two given values (a,b) )
make sure, your outlets (TextA, TextB) and your IBAction (randomInBetweenAction) are wired up correctly, remove
#IBAction func TextAAction(sender: AnyObject) {
a = UInt32(TextA.text!)!
}
and
#IBAction func TextBAction(sender: AnyObject) {
b = UInt32(TextB.text!)!
}
and try this function on button tap:
#IBAction func randomInBetweenAction(sender: AnyObject) {
a = UInt32(TextA.text!)!
b = UInt32(TextB.text!)!
NSLog("a=%ld, b=%ld", a, b)
randomInBetween()
NSLog("\(randomInBetween)")
unHide()
Label.text = "\(randomNumberInBetween)"
NSLog("\(randomInBetween)")
}

Swift - Printing out the current value of a number

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)"
}
}

Simple, algorithm that buy something in Swift Coding

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

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.

Resources