I'm working on a simple guessing game app, just to get more comfortable with Swift and Xcode. I have been able to input within userInput and get it to print a message to the console, however when I try to get it to print my input to usersGuess(which is a label), I can not figure it out.
Here's my code within a single view application via Xcode:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
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 buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
I'm sure this is simple, but I am scratching my head lol.
#IBAction func userInput(sender: UITextField) {
println("This is working")
usersGuess.text = sender.text
}
Although I am still new to iOS dev and Swift, I think you could also take a look at the use of delegate in this tutorial Apple provides. I guess it might be the code didn't resign your text field's first-responder status. Hence, the usersGuess could not update. (Anyone who knows how this work please leave a comment.)
To do this, basically
Create an outlet for the UITextField that receives user's input, say, usersInput.
Set ViewController as a delegate of usersInput, which will
Resign the first-responder status of usersInput when the Return button on the keyboard is pressed.
Update the text of usersGuess.
Code here:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
#IBOutlet weak var usersInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set ViewController as a delegate
usersInput.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Here are the callback functions for usersInput
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
usersGuess.text = textField.text
}
#IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
Related
Good morning!
I have an "unable to load" problem in my iOS widget. I've read a lot of about the "unable to load" message but nothing fixed my problem. I'm not sure but I think my problem is to refresh the widget after changing my content.
My widget has one button and one label. If the user press the button the text from the label will changed - in this moment the widget shows "unable to load". Just a milisecond after pressing the button.
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var segment_att: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.NewData)
}
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
#IBAction func button_ae(sender: AnyObject) {
let tableviewclass = TodayTableViewController()
tableviewclass.newData()
}
}
Important is that the label is shown in a TableViewCell of a TableViewController. So the TableViewController is embeded in the ViewController within a Container... The listener from the button call the method newdata() of the file of the TableViewController.
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
let meldung: String = "test"
label1.text = meldung
}
func newData() {
let meldung: String = "new test"
label1.text = meldung
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The code is really simple and basic - so I'm wondering about the problem in this simple mechanism. I hope you can help me!
Thanks at all!
Your code assumes that label1 has been set when newData() is called, even immediately after the constructor is called.
Try using this optional chaining syntax instead, which will quietly fail if the property is nil:
import UIKit
import NotificationCenter
class TodayTableViewController: UITableViewController, NCWidgetProviding {
#IBOutlet var table: UITableView!
#IBOutlet var label1: UILabel!
var meldung: String = "test" // <-- meldung is property
override func viewDidLoad() {
super.viewDidLoad()
init()
}
func init() {
label1?.text = melding // <-- optional chaining
}
func newData() {
melding = "new test" // <-- set the class property
label1?.text = meldung // <-- optional chaining
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and instead of calling newData(), you might instead just set the meldung property, e.g.:
tableviewclass.meldung = "new test"
as your viewDidLoad() will take care of setting the UILabel text from the property
I am in need of a calendar view for my app. I have decided to use CVCalendar via pod file. However the issue is I can get the days to appear (Sun-Sat) but not the dates (1-19...).
In my Controller file I have:
import UIKit
import CVCalendar
class ViewController: UIViewController, CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
#IBOutlet weak var calendarView: CVCalendarView!
#IBOutlet weak var menuView: CVCalendarMenuView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
calendarView.commitCalendarViewUpdate()
menuView.commitMenuViewUpdate()
}
func presentationMode() -> CalendarMode {
return CalendarMode.MonthView
}
func firstWeekday() -> Weekday {
return Weekday.Sunday
}
}
here is the full code
import UIKit
import CVCalendar
class TabEventsViewController: UIViewController, CVCalendarMenuViewDelegate, CVCalendarViewDelegate {
#IBOutlet weak var viewCalendar: CVCalendarView!
#IBOutlet weak var menuViewCalendar: CVCalendarMenuView!
func presentationMode() -> CalendarMode {
return CalendarMode.MonthView
}
func firstWeekday() -> Weekday {
return Weekday.Monday
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
viewCalendar.commitCalendarViewUpdate()
menuViewCalendar.commitMenuViewUpdate()
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.layoutIfNeeded()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
here are some tips:
make sure you set delegates (2 for calendar view and 1 for calendar menu)
make sure your uiview does not have negative horizontal spacing value
Did you hook up the delegate for the calendar (date) part? Delegate setup
You're delegating the rendering to your ViewController
You need to drag to the yellow view controller nub.
If you have done what Phil.Ng said, and it still doesn't work, there might be something wrong with constraints. Try adding:
self.view.layoutIfNeeded()
in viewDidLoad.
I had this issue too until I found this answer on the GitHub issues. If you change your horizontal constraints on your storyboard to positive values, it should work.
I'm trying to make a kind of conditional segue through the "shouldPerformSegueWithIdentifier" override function, and it reports the error "Method does not override any method from its superclass" and tells me to remove the "override" word. When I do so, it reports the next error:
"Method 'shouldPerformSegueWithIdentifier(:sender:)' with Objective-C selector 'shouldPerformSegueWithIdentifier:sender:' conflicts with method 'shouldPerformSegueWithIdentifier(:sender:)' from superclass 'UIViewController' with the same Objective-C selector".
This is my code now:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
//MARK: Propierties
#IBOutlet weak var whateverLabel: UILabel!
#IBOutlet weak var errorMessageLabel: UILabel!
#IBOutlet weak var textFieldRandomWord: UITextField!
//MARK: Actions
#IBAction func printWhatever(sender: UIButton) {whateverLabel.text = "Whatever"
}
#IBOutlet weak var goOnButton: UIButton!
override func shouldPerformSegueWithIdentifier(identifier: String, sender: UIButton?) -> Bool {
if identifier == "firstsegue" && sender == goOnButton { // you define it in the storyboard (click on the segue, then Attributes' inspector > Identifier
if textFieldRandomWord.text == "Whatever" {
errorMessageLabel.textColor = UIColor .redColor()
errorMessageLabel.text = "*** NOPE, segue wont occur"
return false
}
else {
errorMessageLabel.text = "*** YEP, segue will occur"
}
}
// by default, transition
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textFieldRandomWord.delegate = self //ViewController is textFieldRandomWord's delegate, so "self" reffers to itself (ViewController)//
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
//Hide the keyboard.
textFieldRandomWord.resignFirstResponder()
return true}
func textFieldDidEndEditing(textField: UITextField){
whateverLabel.text = textFieldRandomWord.text}
//Above, the textFieldShouldReturn function makes the text field inactive when return (enter) is pressed. The last function gets activated automatically when this happens.//
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
sender should be AnyObject?. You can't change type in methods declaration to fit your current need like that. You have to check inside the function if your sender is UIButton.
I'm attempting to make an app that checks the text in a UITextField and does something in response to what is written in it. I could possibly have the user type their input and then press a button. I want to be able to check the text field constantly without the application becoming unresponsive.
What you have to do is to attach an IBAction to your UITextField Sent Events Editing Changed:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var strLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func editingChanged(sender: UITextField) {
strLabel.text = sender.text
}
}
I'm using delegation to pass back information from a view controller.
This is the method
func writeValueBack(value: String) {
self.label.text = value
}
The function gets called and all is great apart from the label doesn't update.
The label has a value and is not returning nil, I checked with this line
println(self.label.text)
It prints the value of 'value'
So that means that the label's text is being set to 'value' but it's not updating.
I even tried using the main thread but no luck
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
I just don't know what the problem is.
Any help would be great.
Protocol:
protocol writeValueBackDelegate {
func writeValueBack(value: String)
}
EDIT:
Code for my view controller:
//
// ViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class ViewController: UIViewController, writeValueBackDelegate {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "MainSegue" {
var vc = segue.destinationViewController as SecondViewController
vc.labelText = textField.text
vc.delegate = self
}
}
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
#IBAction func button(sender: AnyObject) {
self.label.text = textField.text
}
#IBAction func segue(sender: AnyObject) {
performSegueWithIdentifier("MainSegue", sender: self)
}
}
Second ViewController source:
//
// SecondViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
var labelText: String!
var delegate: writeValueBackDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func button(sender: AnyObject) {
label.text = textField.text
}
#IBAction func segueBack(sender: AnyObject) {
var editedText = label.text
performSegueWithIdentifier("SecondSegue", sender: self)
if (delegate != nil) {
delegate?.writeValueBack(editedText)
println("working")
}
}
}
The issue is in your second view controller, which as rdelmar pointed out creates a new instance of your first view controller instead of navigating back to the original instance.
To fix this, you could use dismissViewControllerAnimated:completion instead of performing your second segue. But an unwind segue would lead to simpler code, and can be achieved by adding this to your first view controller:
#IBAction func unwindFromSecond(unwindSegue: UIStoryboardSegue) {
if let secondViewController = unwindSegue.sourceViewController as? SecondViewController {
label.text = secondViewController.label.text
// Or whatever you need to retrieve data from the second controller
}
}
Then in your storyboard create an unwind segue from the second view controller. For example if you have a Dismiss button, control-drag from this button to the Exit icon in your second view controller scene and choosing unwindFromSecond. For detailed steps see the answer to this other question: What are Unwind segues for and how do you use them?
You can now remove the writeValueBackDelegate declaration and associated variables, the writeValueBack method and the second view controller's segueBack method