I have this swift file where I'm trying to create a custom class to handle placeholders for multiple text views on one page. I prefer a separate class because I think its cleaner to handle multiple views. Problem is, the functions textViewDidEndEditing and textViewDidBeginEditing are not being called when those events occur.
I've had it successfully work by setting the PostPage to the delegate, but I don't see how I could do that with multiple UITextViews.
This is my code:
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
#IBOutlet weak var eventDescription: UITextView!
#IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
let ph_descr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = ph_descr
let ph_title = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = ph_title
}
}
class PlaceHolderHandler: NSObject, UITextViewDelegate {
var placeholderShowing = true
var placeholderText = ""
required init(placeholderText: String, textBox: UITextView) {
super.init()
self.placeholderText = placeholderText
textBox.text = placeholderText
textBox.textColor = UIColor.lightGray
}
// MARK:- UITextViewDelegates
public func textViewDidBeginEditing(_ textView: UITextView) {
if self.placeholderShowing {
textView.text = ""
textView.textColor = UIColor.black
self.placeholderShowing = false
}
}
public func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" {
textView.text = "Description"
textView.textColor = UIColor.lightGray
self.placeholderShowing = true
}
}
}
When I select either of the UITextViews, I'd like the corresponding function to be called, and have the text adjust accordingly.
You need to retain phDescr and phTitle as in your code they are local variables so get de-allocated end of viewDidLoad
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
#IBOutlet weak var eventDescription: UITextView!
#IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
var phDescr, phTitle:PlaceHolderHandler!
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
phDescr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = phDescr
phTitle = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = phTitle
}
}
also use camelCase to name variables
I've had it successfully work by setting the PostPage to the delegate, but I don't see how I could do that with multiple UITextView
Off course you can set the delegate for both to PostPage and to know which 1 is intact use
func textViewDidBeginEditing(_ textView: UITextView) {
if textView == eventDescription {
}
else { // eventTitle
}
}
Related
I'm doing a test of a custom keyboard. This is what I need:
It has to have two UITextFields. Cannot be labels.
The keyboard is an embedded UIView.
The default keyboard should be disabled.
It cannot be a keyboard extension.
Not sure why the app is crashing. PS: Not all the keys are on the code yet. Here is an image of what I'm trying to do and the two View Controllers.
Edit: The error is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
First ViewController:
import UIKit
class HomeVC: UIViewController, ButtonTapDelegate {
#IBOutlet var textField1: UITextField!
#IBOutlet var textField2: UITextField!
#IBOutlet var keyboardView: UIView!
var buttonPressed = [String]()
override func viewDidLoad() {
addKeyboard(view: keyboardView)
buttonPressed = [String]()
textField1.inputView = UIView()
textField2.inputView = UIView()
}
func addKeyboard(view: UIView) {
let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
view.addSubview(keyboard.view)
addChild(keyboard)
}
func didTapButton(sender: UIButton) {
if sender.tag == 5 {
textField1.text?.append(contentsOf: " ")
} else if sender.tag == 6 {
textField1.text?.removeAll()
buttonPressed = [String]()
} else {
let val = sender.titleLabel?.text
textField1.text?.append(contentsOf: val!)
}
self.textField1.text = buttonPressed.joined(separator: "")
}
}
Here is the second View Controller:
import UIKit
protocol ButtonTapDelegate {
func didTapButton(sender: UIButton)
}
class KeyboardVC: UIViewController {
var delegate: ButtonTapDelegate!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttons(_ sender: UIButton) {
delegate.didTapButton(sender: sender)
print(sender)
}
}
var delegate: ButtonTapDelegate!
An implicitly unwrapped optional is essentially a promise that you're definitely going to give the variable a value before you try to access it. The problem in this case is that you haven't done that. Most likely, you want to do this in your first view controller:
func addKeyboard(view: UIView) {
let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
keyboard.delegate = self // Now "delegate" will have a value before the function gets called
view.addSubview(keyboard.view)
addChild(keyboard)
}
I'm trying to make a custom keyboard. But I cannot input info on the active textField. I'm not sure what I'm doing wrong.
PS: The keyboard is in another ViewController and is passing the sender.tag well.
Here is my code:
import UIKit
class HomeVC: UIViewController, ButtonTapDelegate, UITextFieldDelegate {
#IBOutlet var textField1: UITextField!
#IBOutlet var textField2: UITextField!
#IBOutlet var keyboardView: UIView!
var activeField: UITextField?
var delegate: ButtonTapDelegate!
override func viewDidLoad() {
addKeyboard(view: keyboardView)
textField1.inputView = UIView()
textField2.inputView = UIView()
textField1.becomeFirstResponder()
activeField?.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
activeField = textField
}
func addKeyboard(view: UIView) {
let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
keyboard.delegate = self
view.addSubview(keyboard.view)
addChild(keyboard)
}
func didTapButton(sender: UIButton) {
if sender.tag == 8 {
activeField?.text?.append(contentsOf: " ")
} else if sender.tag == 9 {
activeField?.text?.removeAll()
} else {
let val = sender.titleLabel?.text
activeField?.text?.append(contentsOf: val!)
}
}
}
There is a slight problem in your code that is causing your issue. In the comments, you mentioned that didTapButton() is called beforetextFieldDidBeginEditing. This means that actionField is not assigned a value and therefore is nil. Your code in didTapButton() safely unwraps the optional value so that no error is produced, but of course, you cannot append text to a non-existent UITextField.
I was able to fix it after some research with changes to the textFieldDidBeginEditing and didTapButton functions: Here is the full code if anybody wants to choose one textField at a time with a custom keyboard:
import UIKit
class HomeVC: UIViewController, ButtonTapDelegate, UITextFieldDelegate {
#IBOutlet var textField1: UITextField!
#IBOutlet var textField2: UITextField!
#IBOutlet var keyboardView: UIView!
var activeField: UITextField?
var delegate: ButtonTapDelegate!
override func viewDidLoad() {
addKeyboard(view: keyboardView)
textField1.inputView = UIView()
textField2.inputView = UIView()
textField1.becomeFirstResponder()
activeField?.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeField = textField
}
func addKeyboard(view: UIView) {
let keyboard = KeyboardVC(nibName: "KeyboardVC", bundle: nil)
keyboard.delegate = self
view.addSubview(keyboard.view)
addChild(keyboard)
}
func didTapButton(sender: UIButton) {
if textField1 == self.activeField {
if sender.tag == 8 {
textField1.text?.append(contentsOf: " ")
} else if sender.tag == 9 {
textField1.text?.removeAll()
} else {
let val = sender.titleLabel?.text?
textField1.text?.append(contentsOf: val!)
}
return;
}
if textField2 == self.activeField {
if sender.tag == 8 {
textField2.text?.append(contentsOf: " ")
} else if sender.tag == 9 {
textField2.text?.removeAll()
} else {
let val = sender.titleLabel?.text?
textField2.text?.append(contentsOf: val!)
}
return;
}
}
}
In a macOS app with two NSTextView, I am trying to check when a textView changes, so far I have done this:
class ViewController: NSViewController, NSTextViewDelegate {
#IBOutlet var mainTextField: NSTextView!
#IBOutlet var findPanelFindTextView: NSTextView!
func textDidChange(_ notification: Notification) {
print("Hello!") // works only with mainTextField
}
override func viewDidLoad() {
super.viewDidLoad()
mainTextField.delegate = self // for textDidChange
findPanelFindTextField.delegate = self // for textDidChange
}
}
Only the first NSTextView (mainTextField) triggers the method textDidChange.
I already see this question Check if NSTextView has been edited and that implementation works for my first textView but not for my second textView.
Besides the fact that you wrote findPanelFindTextField but it should be findPanelFindTextView, you should check the object that posted the notification, create a textView object from it but cast it from Any to NSTextView and then create a switch to check which textview is posting the notification:
#IBOutlet var mainTextView: NSTextView!
#IBOutlet var findPanelFindTextView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
mainTextView.delegate = self
findPanelFindTextView.delegate = self
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
switch textView {
case mainTextView:
print("mainTextView changed")
case findPanelFindTextView:
print("findPanelFindTextView changed")
default:
break
}
}
I want to allow the user to change gravity based on their input in gravity text field and pass the input from view controller to a SKScene (PhysicsScene) but I don't know how to reload scene so that it would use the custom gravity by the user instead of its default value.
view of simulator
class SimulationViewController: UIViewController, UITextFieldDelegate {
var sceneNode : PhysicsScene?
#IBOutlet weak var heightTextField: UITextField!
#IBOutlet weak var massTextField: UITextField!
#IBOutlet weak var gravityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
sceneNode = PhysicsScene(size: view.frame.size)
gravityTextField.delegate = self
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsPhysics = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 0 {
if let text = textField.text {
sceneNode?.customGravity = Double(text)!
print(text)
}
}
return true
}
Please help! Thank you!
You have 2 options, make a property in your PhysicsScene class
var customGravity : Double = 0{ didSet { physicsWorld.gravity = {CGPoint(x:0 y:CGFloat(customGravity)}}
or drop custom gravity and set it directly
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 0 {
if let text = textField.text {
sceneNode?.customGravity = physicsWorld.gravity = {CGPoint(x:0 y:CGFloat(Double(text)!)}
print(text)
}
}
return true
}
I do not have XCode available to check my typos, you may have to unwrap some variables
I'm developing note app, when the text view is empty the done button should be disabled so user could not be able to save empty notes into data base, else the button should be enabled.
here's my code below, my attempts have failed; how I can solve this problem?
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var done: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
title = note?.text
if (self.textView.text.isEmpty){
done.enabled = false
}
if let noteContent = note
{
textView.text = noteContent.text
}
self.navigationController!.toolbarHidden = false;
}
func textViewShouldBeginEditing(textView: UITextView) -> Bool{
done.enabled = true
return done.enabled
}
Make your view controller conform to UITextViewDelegate protocol
In Interface Builder, connect the delegate on the text view to your view controller.
Add the following function to your view controller:
func textViewDidChange(textView: UITextView) {
if textView == self.textView {
self.doneButton.enabled = !textView.text.isEmpty
}
}
Try to use another delegate method for you're purpose. This is example :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
if (textView.text.isEmpty) {
button.enabled = false
}
}
}
extension ViewController: UITextViewDelegate {
func textView(textView: UITextView, range: NSRange, replacementText text: String) -> Bool
{
if (!textView.text.isEmpty) {
button.enabled = true
} else {
button.enabled = false
}
return true
}
}
Try this in textViewDidChange method:
yourBarButtonItem.isEnabled = !(yourTextField.text?.isEmpty ?? false)