Created
April 10, 2015 20:24
-
-
Save kellyegan/ca3bb5e9b4b1df728cb0 to your computer and use it in GitHub Desktop.
Delegate Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// DelegateChallenge | |
// | |
// Created by Kelly Egan on 4/7/15. | |
// Copyright (c) 2015 Kelly Egan. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class ViewController: UIViewController, UITextFieldDelegate { | |
@IBOutlet weak var zipCodeTextField: UITextField! | |
@IBOutlet weak var cashTextField: UITextField! | |
@IBOutlet weak var lockedTextField: UITextField! | |
@IBOutlet weak var textLockSwitch: UISwitch! | |
let zipDelegate = ZIPTextFieldDelegate() | |
let cashDelegate = CashTextFieldDelegate() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
zipCodeTextField.delegate = zipDelegate | |
cashTextField.delegate = cashDelegate | |
lockedTextField.delegate = self | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func switchChanged(sender: UISwitch) { | |
if( sender.on ) { | |
lockedTextField.placeholder = "Free to type" | |
} else { | |
lockedTextField.placeholder = "Unlock switch to type" | |
lockedTextField.resignFirstResponder() | |
} | |
} | |
//Unless the textLockSwitch is on don't begin editing | |
func textFieldShouldBeginEditing(textField: UITextField) -> Bool { | |
return textLockSwitch.on | |
} | |
} | |
// | |
// ZIPTextFieldDelegate.swift | |
// DelegateChallenge | |
// | |
// Created by Kelly Egan on 4/7/15. | |
// Copyright (c) 2015 Kelly Egan. All rights reserved. | |
// | |
import UIKit | |
import Foundation | |
class ZIPTextFieldDelegate: NSObject, UITextFieldDelegate { | |
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,replacementString string: String) ->Bool { | |
//See what the new text would be | |
var zipCode : NSString = textField.text | |
zipCode = zipCode.stringByReplacingCharactersInRange(range, withString: string) | |
//If the string is an integer (not nil) then check its length is less than or equal to five. | |
if let zipCodeInt = String(zipCode).toInt() { | |
return (zipCode.length <= 5) | |
} else { | |
return false | |
} | |
} | |
} | |
// | |
// CashTextFieldDelegate.swift | |
// DelegateChallenge | |
// | |
// Created by Kelly Egan on 4/7/15. | |
// Copyright (c) 2015 Kelly Egan. All rights reserved. | |
// | |
import UIKit | |
class CashTextFieldDelegate: NSObject, UITextFieldDelegate { | |
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool { | |
let old : NSString = textField.text | |
let new = old.stringByReplacingCharactersInRange( range, withString: string ); | |
let formatter = NSNumberFormatter() | |
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle | |
var cents = 0 | |
//Strip out everything but digits | |
for character in new { | |
if let digit = String(character).toInt() { | |
cents = cents * 10 + digit | |
} | |
} | |
//Convert int cents to NSNumber dollars | |
var dollars = NSNumber(double: Double(cents) / 100.0 ) | |
//Format dollars | |
textField.text = formatter.stringFromNumber(dollars) | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment