Disable keyboard in UIwebView textField in swift - swift

I wrote a webApp in html/css/js using another text editor, then copied the text over to the relevant files in an existing Xcode project.
The project runs inside a webView alright "left image", but when I click inside a text field "right image" part of the stock keyboard covers the 2 bottom buttons.
I expected to see a fully fledged keyboard, but I really would like to disable it all together, because I will provide my own later.
How to disable the stock keyboard when a webView textField is tapped in this case?
Swift 2.1 and Xcode 7.2.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainWV: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//load a file
let localfilePath = NSBundle.mainBundle().URLForResource("index", withExtension: "html");
let myRequest = NSURLRequest(URL: localfilePath!);
mainWV.loadRequest(myRequest);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Try make use of UIView superclass (superclass of UIWebView as well ass super-super of UITextField) property .userInteractionEnabled:
myWebViewTextField.userInteractionEnabled = false
Alternatively, if this is not what you're looking for, try the property .enabled from superclass UIControl of UITextField:
myWebViewTextField.enabled = false
UIView.userInteractionEnabled Property
A Boolean value that determines whether user events are ignored and
removed from the event queue.
From the Language Reference for UIVIew.
UIControl.enabled Property
A Boolean value that determines whether the receiver is enabled.
From the Language Reference for UIControl.

Related

How do you get a text field input in you view controller code?

I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):
import UIKit
class ViewController: UIViewController {
#IBOutlet var yeet: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func fuel(_ yeet:UIViewController) -> Int {
if yeet == ("hi") {
print("Nice!")
}
}
}
your textfield show be setup as
#IBOutlet weak var textFeildName: UITextField!
you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.
but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"
after that is done would want to reference the variable of the text field:
example:
#IBAction func TextFieldName(_ sender: Any) {
if(self.TextFeildName.text!.contains("hi")){
print("Nice!")
}
}
On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains
Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.

Following Apple 'Food Tracker' tutorial for Xcode - can't get button to change label text

I'm following official iOS Apps tutorial to make a basic Single View Application in Xcode.
Literally all we have done so far is:
Added a label to the UI and set initial text to 'Meal name:'
Added a textbox to the UI
Added a button to the UI
Then we've added some very simple code to the View Controller declaring the label as an outlet and a button action which, when invoked, should change the label's text to Default Text.
My ViewController code is now identical to the tutorial code namely:
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: Actions
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
The problem is that when I press the button in simulator, I get an error message saying an implicitly unwrapped optional value is nil. (app launches fine, it's just when pressing the button)
As I understand it this means something is blank that can't be, but the only optionals I have are:
The textbox, which isn't blank because before I press the button I write 'biscuits' or something in it
The label text, which isn't blank because it's set to 'Meal name:' by default
I really can't work out what supposedly has a nil value that is triggering this error?
As I understand it this means something is blank that can't be
No , This means you need to make sure outlet
mealNameLabel.text = "Default Text" // here mealNameLabel is nil
is connected to the label in IB

Enable user interaction of a button SWIFT

OK, so lets assume the button created below is greyed out and user interaction disabled.
How do I enable the button from anywhere else.
I know how to disable the button from inside the button function, sender.enabled = false, but I don't want to disable it from there.
I want to re-enable the user interaction from outside the button function.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func loadButton(sender: UIButton) {
//i wan to enable interaction of this button via override func and NSUserDefaults
}
}
In iOS and OS X development you have so-called outlets that are variables pointing to UI elements. You can read more about them here.
To declare an outlet, you prefix your variable with #IBOutlet like so:
#IBOutlet weak var button: UIButton!
and then you need to connect your outlet to the button in your xib file. That can be done in several ways, but if you have declared a variable like the above, you can:
go to your storyboard
in the document outline hold down the control button
while holding down the control button, you drag from your files owner or ViewController to the UIButton you'd like to connect to
select the outlet you'd like to connect to (button in this case)
As shown here
Once that is in place, you are free to use enable your button (and much more) in your code, for instance:
func viewDidLoad() {
super.viewDidLoad()
button.enabled = false
}
You can read more about IBOutlets here
And here is another way to connect outlets (this is how I prefer to connect outlets actually, you define and connect in one go)
Hope that helps

Menubar with Storyboard - validateMenuItem not get called

I'm trying to setup a menubar Application using storyboard but my validateMenuItem method not get called.
I will try to explain what i did.
First i dragged a Menu Item in my Application Scene. Then one Object for my MenuController. Created a MenuController (MenuController.swift) and filled it with code. Back in my storyboard I set my Menu delegate to MenuController and MenuController Outlet to Menu. (I'm not totally sure whether i have set the delegates correctly.)
When i start the app, the menu icon appears and the first item title is set to test. But when i'm clicking the icon the validateMenuItem method not get called.
MenuController.swift
import Cocoa
class MenuController: NSObject {
var statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
#IBOutlet weak var statusMenu: NSMenu!
#IBOutlet weak var item1: NSMenuItem!
override func awakeFromNib() {
print("awakeFromNib")
self.item1.title = "Test"
let icon = NSImage(named: "menubarIcon")
statusItem.image = icon
statusItem.menu = statusMenu
}
override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
print("validateMenuItem")
return true
}
}
Storyboard Menu Delegates
(source: picr.de)
Storyboard MenuController Delegates
(source: picr.de)
Has anybody an idea?
Greets from Austria!
The menu/UI validation mechanism does not query the menu's delegate but uses the item's target to determine the enabled state instead.
If the target is not explicitly set, it walks the responder chain.
To get basic validation, you have to make sure that the following things are setup:
"Auto Enables Items" is checked in Interface Builder (on by default)
You control-dragged the menu's action to the first responder
The menu's action is implemented in a class that is part of the responder chain (e.g. a view controller that manages a set of actions for your UI)
A basic implementation of validateUserInterfaceItem: could look like the following for an item with an action selector called test:
func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool
{
if anItem.action() == Selector("test:") {
print("validating item \(anItem)")
return true
}
return true
}

Swift - Trouble with TextField.clearsOnBeginEditing

override func viewDidLoad() {
super.viewDidLoad()
self.enterNumberTextField.clearsOnBeginEditing = true
}
Setting this bool true does not persist in later code, and i do not reset it in any of my code. Setting the bool does not force the text box to clear when the user puts the cursor in it. I check the value of the bool and it is true on the first pass through code, though the text box does not clear. On subsequent passes through code the bool is reset to false. not by my code.
I have also tried putting this line of code into the IBAction function where all the processing is done in this program. Same result. Does not clear text box and is reset to false inside the if {} where all the work is done.
Can someone tell me why this is happening, and if there is a place where this bool should be set to true such that it persists throughout the program's execution.
From the Swift documentation:
Even if this property is set to true, the text field delegate can override this behavior by returning false from its textFieldShouldClear: method.
Does your view adhere to the UITextFieldDelegate protocol? Do you implement the textFieldShouldClear method? This sounds like it could be the source of your problem.
EDIT:
What happens if you do something like this:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.clearsOnBeginEditing = true
textField.delegate = self
}
func textFieldShouldClear(textField: UITextField) -> Bool {
return true
}
}
That is, add the UITextFieldDelegate protocol at the top, and then implement textFieldShouldClear so it always returns true. This may not be what you ultimately want, but it might solve the issue. Check out the Swift documentation for more info on protocols and delegation.
The problem was I only had one text field and was never giving up focus. I added another text field to transfer focus to , now when I put focus back in the text field of interest, it does clear. Simple, must transfer focus for that method to work.