How can I add several UIStepper Values to one Label? - swift

I'm working right now on my first Swift project. I've got 2 stepper and one label - both stepper are sending their values to it. How can I add the value of the second stepper to the label, in which the value of the first stepper is already? Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
valueLabel.text = Int(sender.value).description
}
}
Thank you!

If you want to combine the two values to ONE String and show this String on your Label, than you have to create a new function that does this for you. I added such a function to your code:`
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
stepper.wraps = true
stepper.autorepeat = true
stepper.maximumValue = 10000
stepper2.wraps = true
stepper2.autorepeat = true
stepper2.maximumValue = 10000
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var valueLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBAction func stepperValueChanged(sender: UIStepper) {
// valueLabel.text = Int(sender.value).description
addValuesToASumAndPutItIntoTheLabel()
}
#IBOutlet weak var stepper2: UIStepper!
#IBAction func stepper2ValueChanged(sender: UIStepper) {
// valueLabel.text = String(sender.value)
addValuesToASumAndPutItIntoTheLabel()
}
func addValuesToASumAndPutItIntoTheLabel() {
let summe : Int = Int(stepper.value + stepper2.value)
valueLabel.text = summe.description
}
}`

Related

Animation only works on one button (ripple view)

Site used: https://material.io/develop/ios/components/ripple/
Here is my code:
import MaterialComponents.MaterialRipple
class ViewController: UIViewController {
// let rippleView = MDCRippleView()
let rippleTouchController = MDCRippleTouchController()
#IBOutlet weak var playBtn: UIButton!
#IBOutlet weak var levelsBtn: UIButton!
#IBOutlet weak var topicsBtn: UIButton!
#IBOutlet weak var settingsBtn: UIButton!
#IBOutlet weak var instaBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//This works for instabtn (lastone)
// rippleTouchController.rippleView.rippleColor = .lightGray
// rippleTouchController.addRipple(to: playBtn)
// rippleTouchController.addRipple(to: levelsBtn)
// rippleTouchController.addRipple(to: topicsBtn)
// rippleTouchController.addRipple(to: settingsBtn)
// rippleTouchController.addRipple(to: instaBtn)
}
override func viewDidAppear(_ animated: Bool) {
// Currently only works for playbtn
rippleTouchController.rippleView.rippleColor = .lightGray
rippleTouchController.addRipple(to: levelsBtn)
rippleTouchController.addRipple(to: topicsBtn)
rippleTouchController.addRipple(to: settingsBtn)
rippleTouchController.addRipple(to: instaBtn)
rippleTouchController.addRipple(to: playBtn)
}
}
I've tried to put the code in viewDidAppear but it doesn't make a difference. Any thoughts?
Foreach button you have to create a MDCRippleTouchController.
let rippleTouchController = MDCRippleTouchController()
let rippleTouchController2 = MDCRippleTouchController()
let rippleTouchController3 = MDCRippleTouchController()
let rippleTouchController4 = MDCRippleTouchController()
let rippleTouchController5 = MDCRippleTouchController()
override func viewDidAppear(_ animated: Bool) {
rippleTouchController.rippleView.rippleColor = .lightGray
rippleTouchController.addRipple(to: levelsBtn)
rippleTouchController2.addRipple(to: topicsBtn)
rippleTouchController3.addRipple(to: settingsBtn)
rippleTouchController4.addRipple(to: instaBtn)
rippleTouchController5.addRipple(to: playBtn)
}

comparing local images swift

"Optional type Bool cannot be used as a boolean; test for !nil instead"
Is the error I'm getting
I'm trying to make a "slot machine" app, very basic,
You press the UIButton, and the three images should all change, randomly, if the 3 matches, print "You won!"
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
#IBOutlet weak var rollBtn: UIButton!
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.
}
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll = ImgArray().getRandomImage()
img1.image = randomRoll
img2.image = randomRoll
img3.image = randomRoll
if (img1.image! == img2 && img3) {
print("You won!")
}
}
}
If your images are in an array, why not get the indexes of the images and compare that. Or you could convert the images to base64 strings and compare those.
Edit: Not knowing much about your ImgArray class, this may work for you:
#IBAction func onRollPress(sender: AnyObject) {
let randomRoll1 = ImgArray().getRandomImage()
let randomRoll2 = ImgArray().getRandomImage()
let randomRoll3 = ImgArray().getRandomImage()
img1.image = randomRoll1
img2.image = randomRoll2
img3.image = randomRoll3
let imgIndex1 = ImgArray().indexOf(randomRoll1)
let imgIndex2 = ImgArray().indexOf(randomRoll2)
let imgIndex3 = ImgArray().indexOf(randomRoll3)
if (imgIndex1 == imgIndex2 && imgIndex3) {
print("You won!")
}
}

How to make a Sprite move when comes into contact with another object

This is my first time posting on stack overflow, please let me know if I am doing something wrong. My task is simple, I want to send the sprite moving at a random distance when it comes into contact with the tennis racker / object. I need help with the second part of my task. Can anyone please help me? I am relatively new to Swift, so please keep that in mind.
class ViewController: UIViewController {
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var tennisRacket: UIImageView!
#IBOutlet weak var moveRacket: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var scoreText: UILabel!
var tennisScene = SKScene()
var tennisBallSprite = SKSpriteNode(imageNamed: "tennisball")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// hide or show elements
playButton.hidden = false
tennisRacket.hidden = true
moveRacket.hidden = true
scoreLabel.hidden = true
scoreText.hidden = true
tennisBallSprite.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
#IBAction func playPressed(sender: UIButton) {
playButton.hidden = true
tennisRacket.hidden = false
moveRacket.hidden = false
tennisBallSprite.hidden = false
tennisBallSprite.position = CGPointZero
tennisBallSprite.size = CGSize(width: 20, height: 20)
tennisBallSprite.physicsBody?.affectedByGravity = true
tennisBallSprite.physicsBody?.allowsRotation = true
tennisScene.addChild(tennisBallSprite)
}
#IBAction func hitBall(sender: UIButton) {
self.tennisRacket.transform = CGAffineTransformMakeRotation((-15 * CGFloat(M_PI)) / 180.0)
delay(1/5) {
self.tennisRacket.transform = CGAffineTransformIdentity
}
}
}

Delegate Method is not called in Swift?

I want to pass a Bool value from on view controller to another without the help of segues. So i referred & got Delegates.
I have applied delegates in my App. But the Delegate method is not being called. I don't know where i am making the mistake.
So Please help me.
MainViewController
class MainViewController: UIViewController, WriteValueBackDelegate {
#IBOutlet weak var LoginButton: UIButton!
var LoggedInL :Bool?
override func viewDidLoad() {
super.viewDidLoad()
}
func writeValueBack(value: Bool) {
println("Delegate Method")
if (value == true){
LoginButton.setTitle("My Profile", forState:UIControlState.Normal)
}
}
Second View Controller
class LoginController: UIViewController {
#IBOutlet weak var LoginLabel: UILabel!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var pwd: UITextField!
var LoggedInL :Bool?
var mydelegate: WriteValueBackDelegate?
override func viewDidLoad() {
super.viewDidLoad() }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onSubmit(sender: AnyObject) {
Alamofire.request(.GET, "http://www.jive.com/index.php/capp/user_verification/\(email.text)/\(pwd.text)")
.responseJSON { (_, _, data, _) in
println(data)
let json = JSON(data!)
let name = json["first_name"].stringValue
let status = json["valid_status"].intValue
println(status)
var e = self.email.text
println(e)
self.LoginLabel.text = "Hey \(name)!"
if status == 1{
println("Correect")
self.LoggedInL = true
self.mydelegate?.writeValueBack(true)
}else {
self.LoggedInL = false
println("Error")
}
}
navigationController!.popViewControllerAnimated(true)
}
}
protocol WriteValueBackDelegate {
func writeValueBack(value: Bool)
}
you didn't initialize the delegate, and no need to, delegates are usually for async callbacks. do that instead:
class MainViewController: UIViewController {
static var sharedInstace : MainViewController?;
#IBOutlet weak var LoginButton: UIButton!
var LoggedInL :Bool?
override func viewDidLoad() {
super.viewDidLoad()
MainViewController.sharedInstace = self; //this is better from init function
}
func writeValueBack(value: Bool) {
println("Delegate Method")
if (value == true){
LoginButton.setTitle("My Profile", forState:UIControlState.Normal)
}
}
}
in login view controller
MainViewController.sharedInstance?.writeValueBack(true)
In MainViewControlleryou need a reference of the LoginController instance maybe with an IBOutlet and then set the delegate in viewDidLoad
#IBOutlet weak var loginController : LoginController!
override func viewDidLoad() {
super.viewDidLoad()
loginController.mydelegate = self
}

Navigation Controller Error

I have a navigation controller and I want the title to have a custom font. I have tried to do this but when it runs I get Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP.subcode=0x0)
Here is my code.
import UIKit
class PriceCheckSpreadsheetViewController: UIViewController {
#IBOutlet weak var SpreadsheetView: UIWebView!
#IBOutlet weak var Loading: UIActivityIndicatorView!
#IBOutlet weak var BackButton: UIBarButtonItem!
#IBOutlet weak var ForwardButton: UIBarButtonItem!
#IBOutlet weak var NaviBar: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = "http://www.backpack.tf/pricelist/spreadsheet"
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
SpreadsheetView.loadRequest(request)
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "TF2Build", size: 12)!]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ : UIWebView) {
Loading.startAnimating()
NSLog("Loading")
}
func webViewDidFinishLoad(_ : UIWebView) {
Loading.stopAnimating()
NSLog("Done")
if SpreadsheetView.canGoBack {
BackButton.enabled = true
}
else {
BackButton.enabled = false
}
if SpreadsheetView.canGoForward {
ForwardButton.enabled = true
}
else {
ForwardButton.enabled = false
}
}
#IBAction func Reload(sender: AnyObject) {
SpreadsheetView.reload()
}
#IBAction func Back(sender: AnyObject) {
SpreadsheetView.goBack()
}
#IBAction func Forward(sender: AnyObject) {
SpreadsheetView.goForward()
}
}