Can't make sens of UIStepper (S) - swift

I'm trying to make an app to count different tokens in a board game. I thought it would be easy peasy, but not quite.
I succeeded in adding one UIStepper to count one kind of token, but I don't know how to add the other four. This is what I've got so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
}
I got one action and two outlets.

import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBOutlet weak var theLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBOutlet weak var theLabel3: UILabel!
#IBOutlet weak var stepper3: UIStepper!
#IBOutlet weak var theLabel4: UILabel!
#IBOutlet weak var stepper4: UIStepper!
#IBOutlet weak var theLabel5: UILabel!
#IBOutlet weak var stepper5: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
#IBAction func StepperTapped2(sender: UIStepper) {
self.theLabel2.text = "\(self.stepper2.value)"
}
#IBAction func StepperTapped3(sender: UIStepper) {
self.theLabel3.text = "\(self.stepper3.value)"
}
#IBAction func StepperTapped4(sender: UIStepper) {
self.theLabel4.text = "\(self.stepper4.value)"
}
#IBAction func StepperTapped5(sender: UIStepper) {
self.theLabel5.text = "\(self.stepper5.value)"
}
}

Related

How to resize the detailsView after cross button is clicked after the closing view is hidden?

[![when the cross button is clicked, the closingView is hidden.][1]][1]
import UIKit
import IBAnimatable
class ViewController: UIViewController {
#IBOutlet weak var closingView: AnimatableView!
#IBOutlet weak var detailsView: AnimatableView!
#IBOutlet weak var btnClose: UIButton!
#IBOutlet weak var closeViewBtn: UIView!
#IBOutlet weak var scrollView: UIScrollView!
var isCloseButtonTapped = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btnCloseAction(_ sender: Any) {
isCloseButtonTapped = !isCloseButtonTapped
if isCloseButtonTapped{
closingView.isHidden = true
}else{
//
}
}
}
[1]: https://i.stack.imgur.com/CpH2A.png
You can add closingView and detailsView inside a UIStackView. When the closingView is hidden the detailsView will fill up the entire space in the parent UIStackView.

How can I carry out multiple different animations at different times with the same UIButton?

I am creating an animation sequence where when one button is pressed one object fades out. When the same button is pressed again another object fades out. This keeps happening until 8 objects are faded out. I have tried and played around with code and so far have got the code to fade out one object, and two or more at a time, but have not successfully been able to do it as described above. This is my code so far:
import UIKit
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.glassOne.alpha = 1.0
self.glassTwo.alpha = 1.0
}
#IBAction func drunkOne(_ sender: Any){
if self.glassOne.alpha == 1.0{
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{self.glassOne.alpha = CGFloat(0.1)}
)
}
}
Put your glasses into an array glasses and add a property to keep track of nextToFade. Then just fade the glasses[nextToFade] and increment nextToFade by 1:
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
// array to hold the glasses so that they can be indexed
var glasses = [UIImageView]()
// index of next glass to fade
var nextToFade = 0
override func viewDidLoad() {
super.viewDidLoad()
// put the glasses into the array
glasses = [glassOne, glassTwo, glassThree, glassFour,
glassFive, glassSix, glassSeven, glassEight]
// make sure they're all visible
glasses.forEach { $0.alpha = 1.0 }
}
#IBAction func drunkOne(_ sender: Any) {
// make sure not to index beyond the end of the array
guard nextToFade < glasses.count else { return }
let glass = glasses[nextToFade]
nextToFade += 1
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{ glass.alpha = 0.1 }
)
}
}

Custom UITableViewCell has no initialiser error on only one of the two created classes. Swift

I'm adding a new TableVIewto the project and I'm also creating a custom class fro the cell. I'm doing as usual : New file/ Cocoa Touch Class / UITableViewCell / name. As soon as I start adding properties I get the error dough properties are declared as!. It doesn't happen on my other custom cell class. Can you see what am I doing wrong with this new class?
No error on this class :
import UIKit
class CalendarTableViewCell: UITableViewCell {
#IBOutlet weak var dayLabel: UILabel!
var cellId: String!
var cellWeekday: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and the new class that makes xCode complain:
import UIKit
class ProductTableViewCell: UITableViewCell {
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var productIDLabel: UILabel!
#IBOutlet weak var productIDInfoLabel: UILabel!
#IBOutlet weak var categoryLabel: UILabel!
#IBOutlet weak var categoryInfoLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameInfoLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var priceInfoLabel: UILabel!
#IBOutlet weak var quantityLabel: UILabel!
#IBOutlet weak var quantityInfoLabel: UILabel!
var productImage: UIImage!
var category: String!
var productId: String!
var name: String!
var price: String
var vendor: String!
var cellId: Int64
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
It's something I overlooked for sure but I can't spot it.
What should I check?
Your var cellId: Int64 is not initialized. In first cell you explicitly specified that you will initialize it before use with exclamation mark, but not in the second cell.

How to use UIButtons in a Cell with Functions?

I created a custom cell added the actions and outlets.
I want to be able to get the cell indexpath when the cell button is pressed.
I am send data to my parse server and I need to the cell indexPath.row so when someone clicks the button the count for likes or dislikes goes up.
My Custom Cell :
class myTVC: UITableViewCell {
#IBOutlet weak var greenUp: UIButton!
#IBOutlet weak var redDown: UIButton!
#IBOutlet weak var imageViewTVC: UIImageView!
#IBOutlet weak var upVote: UILabel!
#IBOutlet weak var downVote: UILabel!
#IBOutlet weak var saveButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
#IBAction func voteDownPressed(sender: UIButton) {
print("downVote")
}
#IBAction func voteUpPressed(sender: UIButton) {
print("upVote")
}
}

Swift- Adding Two UI Numbers then Output in Label

I can do this in C++/C, Objective C and Playground. I cannot get this to work in Swift when I try to make the App.
Can someone help out please?
//
// ViewController.swift
// Simple Mathemtical Functions_Swift
//
// Created by on 7/01/2015.
// Copyright (c) 2015 . All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var labelResult: UILabel!
#IBOutlet var Number2: UILabel!
#IBOutlet var Number1: UILabel!
#IBOutlet var field1: [UITextField]!
#IBOutlet var field2: [UITextField]!
#IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\()"}
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.
}
}
If field1 and field2 and just UITextFields then [UITextField]! should be changed to UITextField!. [UITextField] indicates an array of UITextFields.
#IBOutlet var labelResult: UILabel!
#IBOutlet var Number2: UILabel!
#IBOutlet var Number1: UILabel!
#IBOutlet var field1: UITextField!
#IBOutlet var field2: UITextField!
#IBAction func Calculate(sender: AnyObject) {
var a = (field1.text as NSString).floatValue
var b = (field2.text as NSString).floatValue
var sum = a + b
labelResult.text = "\(sum)"
}
Maybe you're having trouble because field1.text is a String? not an NSString.
#IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\((field1.text as NSString).floatValue + (field2.text as NSString).floatValue)"
}