I have some very basic code which reads the text from a textfield when the user presses a button, and updates the text on a label. I have the type of the input field set to number pad.
import UIKit
class ViewController: UIViewController {
#IBOutlet var userGuessTextField: UITextField!
#IBOutlet var resultLabel: UILabel!
#IBAction func guess(sender: AnyObject) {
let randomNumber = String(arc4random_uniform(11))
if randomNumber == userGuessTextField.text {
resultLabel.text = "You're right"
} else {
resultLabel.text = "Wrong! It was a \(randomNumber)"
}
}
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.
}
}
When I trigger my action, xCode throws this error:
Can't find keyplane that supports type 4 for keyboard
iPhone-PortraitTruffle-NumberPad; using
675849259_PortraitTruffle_iPhone-Simple-Pad_Default
EDIT:
Turns out this is not an error at all, but a simple logging statement. I must have accidentally set a breakpoint in my action, giving it the look and feel of a crash. Thank you, first responder.
Laugh uncontrollably and just keep going. It's not an error. It's just a bit of fluff dropped into the console. Ignore it.
It can happened sometime when you update xocode testing on Simulator you can try this fix:
iOS Simulator -> Hardware -> Keyboard -> Connect Hardware Keyboard
This is not an error. It just says cannot find keyplane..
You can switch to simulator keyboard from Hardware -> Keyboard -> Toggle Software Keyboard or simply press cmd + shift + k
You can even ignore this ;-)
Related
When I am running Xcode with below codes it says "build succeeded" but on the simulator nothing is being appeared and I don't know why. Please kindly advise what to fix.
import UIKit
class ViewController: UIViewController {
let numberOfPlanets = 8
let diameterOfEarth = 24859.92 // in miles, where pole to pol
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print("Welcome to solar system!")
print("There are \(numberOfPlanets) to explore")
print("You are currenly on the Earth, which has a circumstance of \(diameterOfEarth) miles")
firstTextView.text = "Welcome to solar system!"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var firstTextView: UITextView!
}
added.
thank you Shades enter image description herefor your advice. I checked all you advised but the console is not still printing. I changed the name of "ViewController.swift" to "main.swift" to remove an error saying "expressions are not at the top level". probably it is the reason? I added the screenshot of mine for better understanding. please help.
I'm making a text-based iOS game which uses a single view consisting of a UITextView (scrollable, not editable) and a UITextField. I've made it so when I press return while editing the textField, the keyboard closes and the text of the textField is added to the textView. However, the text of the UITextView doesn't appear at all. The background color still appears, but nothing else, no matter how I try to display it. I've already tried setting the font size and color.
Here's code from the ViewController that handles both UI components.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
// outlets / actions
#IBOutlet weak var outputView: UITextView!
#IBOutlet weak var inputField: UITextField!
var uni: Universe? // a custom class that contains the game's data
override func viewDidLoad() {
//outputView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
inputField.delegate = self
outputView.delegate = self
inputField.text = "press return for help"
outputView.text = ""
outputView.textColor = UIColor.greenColor()
outputView.frame = self.view.frame
// The next 3 lines make a new Universe object.
uni = Assembler.build(outputView)
uni!.setUni()
uni!.setParser()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
uni!.player.viewLocation()
}
// delegate method; this should do 3 things:
// 1. remove the keyboard
// 2. call uni.parser.read(text)
// 3. remove the text in the field
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
outputView.text! += textField.text! + "\n"
uni!.parser!.read(textField.text!) // This function interprets the text from textField and prints something appropriate in the textView.
textField.text = ""
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// The rest of the code makes the whole frame shift up/down when the keyboard appears/disappears to keep the UITextField visible.
}
Any help at all would be greatly appreciated. The similar questions on this site haven't given me anything to go on.
I just want to modify the height of a UITextView.
The log resulting from the code below, says that in fact, the
height changed from 30 to 400:
println(txtresponses.frame.height) // returns 30
var newFrame:CGRect=txtresponses.frame
newFrame.size.height=400
txtresponses.frame=newFrame
println(txtresponses.frame.height) // returns 400
However, visually, the UITextView "txtresponses" remains with the same size.
I am new to Swift and Xcode, so all my tricks are already exhausted here, and I dont know if it is an iOS version issue, or some typical Xcode whim.
What is the correct way to modify a UITextView ´s height?
Make sure to call txtresponses.frame=newFrame in the main thread.
dispatch_async(dispatch_get_main_queue()) {
txtresponses.frame=newFrame
}
All UI updates must be done from the main thread.
Its not work because I think you are using Autolayout with constraint. Please check below url which may help you - Change height constraint programmatically
Might be issue Autolayout. u just remove the autolayout and check it will work. Check below code i hope it will help you.
Example :
import UIKit
class ViewController: UIViewController {
#IBOutlet var textView: UITextView!
#IBOutlet var butt: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
textView.backgroundColor = UIColor.lightGrayColor()
// 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 buttonAction(sender: UIButton) {
var newFrame:CGRect=textView.frame
newFrame.size.height=400
textView.frame=newFrame
}
}
Screen 1:
Screen 2:
Today I was following this tutorial: http://youtu.be/9rq4P-L-z8U. However, when it came to the part where it showed how to close the keyboard after pressing the button, I followed the instructions, but when I started the simulator, the keyboard simulator never appeared on the screen.
How can I fix this?
This is my code:
class ViewController: UIViewController {
#IBOutlet var TextField: UITextField!
#IBOutlet var Label: UILabel!
#IBAction func ButtonMethod(sender: AnyObject) {
Label.text = TextField.text
TextField.resignFirstResponder()
}
}
#IBAction func ButtonMethod(sender: AnyObject) {
self.view.endEditing(true)
}
This will remove the keyboard from your screen when you press on a button
EDIT: Since your title is different than your question this is the answer:
Uncheck connect hardware keyboard
if when you start edit text box and keyboard is not showing than on simulator functions , go to Hardware->KeyBoard->Connect HardWare KeyBoard and uncheck it!!
otherwise in button action just write
YourTextField.resignFirstResponder = true
may this will help you also check for textfield delegate connected or not
I've created a custom UIViewController with one UITextField on Storyboard. On viewDidLoad, I set the UITextFIeld to becomeFirstResponder, nothing happened (no keyboards popped up).
I then tried calling resignFirstResponder(), but it returned false. Next I tried to find who the first responder is through looping all the subviews using the code over # Get the current first responder without using a private API. It turns out none of the sub views are the first responder.
My ViewController in storyboard
My Code
class BLTestViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var tf: UITextField
override func viewDidLoad() {
super.viewDidLoad()
tf.delegate = self
tf.becomeFirstResponder()
// Do any additional setup after loading the view.
}
}
As simple as it gets...WHY ISN'T THE KEYBOARD COMING UP!!! One thing I do have on is auto layout, but I'm not sure if that affects the keyboard popping up.
Swift 4 it worked for me
try it
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textView.becomeFirstResponder()
}
I just tested it with a textfield by calling self.tf.becomeFirstResponder() indside viewDidLoad() function and its working absolutely fine. You'll need to toggle your keyboard by pressing command + K as nflacco just pointed out and disable the hardware keyboard in the simulator as:
iOS Simulator -> Hardware -> Keyboard
Uncheck "Connect Hardware Keyboard"
I think
class BLTestViewController: UIViewController, UITextFieldDelegate {
#IBOutlet var tf: UITextField
override func viewDidLoad() {
super.viewDidLoad()
tf.delegate = self
self.focustf()
// Do any additional setup after loading the view.
}
func focustf(){
self.tf.becomeFirstResponder()
}
}
Swift 4 and iOS 11
In my case, no mater what I tried, I was not able to set first responder until I tried this.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.isKind(of: YOURTABLEVIEWCELL.self) {
if let yourCell = cell as? YOURTABLEVIEWCELL{
yourCell.yourUiTextField.becomeFirstResponder()
}
}
}
Hope this helps someone stuck with the same problem.
Try setting it after a delay:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
textField.becomeFirstResponder()
}
sometimes the wrong is the time in render the view
If you are using the iOS Simulator, press ⌘ command + shift + K to open the keyboard.