textField to LabelView live by swift 2 [duplicate] - swift

This question already has answers here:
UITextField text change event
(22 answers)
Closed 6 years ago.
how can i what ever i type in textField immediately show it in labelView without button that do it ?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
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.
}
}

It's best to use UITextField's delegate:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
labelView.text = textViewout.text
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
EDIT:
Sorry, it actually is not exactly the best solution. It will be delayed. This should work more efficiently for you:
class ViewController: UIViewController {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.addTarget(self, action: "updateLabel", forControlEvents: .EditingChanged)
}
func updateLabel() {
labelView.text = textViewout.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Xcode 10 button to create mad lips story will not run, I'm new to Xcode

I'm just starting out with Xcode 10 and cannot get this button to work for my mad libs creation. this is just for fun but I would love to know how to fix it. Please look over my code to see why the "create story" button is not active.
// ViewController.swift
// Field Button Fun
//
// Created by Audrey Chao on 8/3/16.
// Copyright © 2016 Maddie Chao. All rights reserved.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var thePlace: UITextField!
#IBOutlet weak var theVerb: UITextField!
#IBOutlet weak var theNumber: UITextField!
#IBOutlet weak var theTemplate: UITextView!
#IBOutlet weak var theStory: UITextView!
#IBAction func createStory(sender: AnyObject) {
theStory.text = theTemplate.text
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<place>", withString: thePlace.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<verb>", withString: theVerb.text!)
theStory.text = theStory.text.stringByReplacingOccurrencesOfString("<number>", withString: theNumber.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
//thePlace.resignFirstResponder()
// 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.
}
}
Have you wired up your button to your IBAction? If they aren't wired up, Xcode won't be able to tell what your button is supposed to do.

error message with IBAction

import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var emailkeyboard: UITextField!
#IBOutlet weak var passwordkeyboard: UITextField!
#IBOutlet weak var myaccountButton: UIButton!
#IBOutlet weak var welcomeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Built in method
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
#IBAction func buttonPressed(sender: UIButton) {
self.emailkeyboard.resignFirstResponder()
self.passwordkeyboard.resignFirstResponder()
}
#IBAction func myaccountbutton(sender: AnyObject) {
FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!, completion: { (user,error) in
}
#IBAction func createaccountButton(sender: AnyObject) {
FIRAuth.auth()?.createUserWithEmail(emailField.text!, password: passwordField.text!) { (user, error) in
if error == nil {
print("User Created")
if (FIRAuth.auth()?.currentUser) != nil
{
self.myaccountButton.alpha = 1.0
}
else
{
self.myaccountButton.alpha = 0.0
self.welcomeLabel.text = ""
}
}
}
}
}
}
I cant figure out where to put the ) I asked before but i am still stuck. Sorry guys for asking again.
I am not sure if theres an error in the code above or with the IBAction itself so I have posted all of the code maybe that will help find the problem.
The signInWithEmail() function call is missing its ending parentheses, as well as an actual closure for the closure argument.

Cancel button only clears the recipient number and does not return back to app?

The cancel button only clears the recipient phone number but does not go back to the app? Any help? Here is the code I'm currently using.....
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
#IBOutlet weak var txtMsg: UITextField!
#IBOutlet weak var txtPhone: UITextField!
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 sndSMS(sender: AnyObject) {
txtMsg.resignFirstResponder()
txtPhone.resignFirstResponder()
let msgVC = MFMessageComposeViewController()
msgVC.body = txtMsg.text!
msgVC.recipients = ["1-206-724-8288"]
msgVC.messageComposeDelegate = self;
self.presentViewController(msgVC, animated: true, completion: nil)
You're assigning the delegate but you are not implementing the method listed here:
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontrollerdelegate/1614061-messagecomposeviewcontroller
func messageComposeViewController(_ controller: MFMessageComposeViewController,
didFinishWithResult result: MessageComposeResult)

How can I add several UIStepper Values to one Label?

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
}
}`

How to send my UITextfield to a variable via an UIButton?

I want to know how can I type something in my UITextfield and then send it to a variable via an UIButton?
When I try it with this code the app automatically crashes and show me this error:
Thread:1 signal SIGABRT
Here's the code:
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var one = String(Heure.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LabelHeure.text = NSDateFormatter.localizedStringFromDate(NSDate(),dateStyle:NSDateFormatterStyle.MediumStyle,timeStyle: NSDateFormatterStyle.NoStyle)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You don't need to cast Heure.text as a string because it is already a string.
Here is the code I used and it worked. After pressing the button, the variable is updated with the text from the textfield. I used a println to verify.
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var myVariable = Heure.text
println(myVariable)
}
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.
}
}