I have this button:
#IBAction func touch(sender: AnyObject) {
println("Hello, world!")
}
and another:
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
I want to disable user touch of "touch" at the start of the app, and enable touch after tapping "toggle", how to realize it by code and in storyboard?
You need #IBOutlets to access the buttons at runtime. In InterfaceBuilder ctrl-drag the button to the top of your custom class file, choose Outlet and give it a descriptive name. It will look like this:
class MyViewController: UIViewController {
#IBOutlet weak var touch: UIButton!
#IBOutlet weak var toggle: UIButton!
// Disable in `viewDidLoad`:
override func viewDidLoad() {
super.viewDidLoad()
self.touch.enabled = false
}
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
self.touch.enabled = true
}
}
Swift 3 syntax
button.isEnabled = true
An update with swift 2 and IOS 10 is self.touch.isEnabled = false.
They have gotten rid of enabled and replaced it with isEnabled.
You can disable the button in the storyboard - uncheck the enabled checkbox.
To enable it in code you should create an outlet and then set the enabled property to true:
button.enabled = true
Solution 1
myButton.isEnabled = false;
Solution 2 //Desperate way to disable button by disabling the user interaction.
myButton.isUserInteractionEnabled = false;
Related
In my storyboard I have a basic view, with a login field. In the storyboard the checkbox for Enable user interaction is checked, and I am also settings this through the code.
Below is the code I'm using for this:
class LoginViewController : UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad();
let closeKeyboardGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard");
self.view.userInteractionEnabled = true
self.view.addGestureRecognizer(closeKeyboardGesture);
}
private func dismissKeyboard() {
print("Called")
if passwordField.selected {
passwordField.resignFirstResponder()
}
if emailField.selected {
emailField.resignFirstResponder()
}
}
}
As you can see it's fairly straightforward, however the dismissKeyboard function is never called. What bothers me is I'm using these gestures elsewhere in my application and they're working fine.
That´s because you have your dismissKeyboard function marked as private. If you are calling a method from a selector and it is private they cannot be called because the method is called from outside.
So remove private from dismissKeyboard and it will work.
Update
If you change the above it will work, what´s not working for you right now is the resignFirstResponder You don´t need to the if-checks since you always want to hide the keyboard when you active the closeKeyboardGesture, so it´s enough to only call self.view.endEditing(true). I have created a sample project for you that has a working example. You can download it from here.
func dismissKeyboard() {
view.endEditing(true)
}
This will do the trick.
I created a custom input view with a couple of UIButtons inside it. All these buttons invoke one single IBAction.
The IBAction is not called although I'am 100% sure I set them properly in IB. To prove that this isn't causing the problem I added a target to one of the buttons programmatically, but the action still won't get invoked.
import UIKit
protocol CashDeskInputDelegate: class {
func didReceiveInput(inputValue: String)
func didReceiveDoneMessage()
}
class CashDeskInputViewController: UIInputViewController {
// Delegate
weak var delegate: CashDeskInputDelegate?
// Actions
#IBAction func buttonTapped(sender: UIButton) {
print("buttonTapped")
guard let valueOfSender = sender.titleLabel?.text else {
dismissKeyboard()
return
}
valueOfSender == "Done" ? delegate?.didReceiveDoneMessage() : delegate?.didReceiveInput(valueOfSender)
}
// Test outlet
#IBOutlet weak var firstButton: UIButton!
override func viewDidLoad() {
// Add an action to the first button for testing purposes.
firstButton.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
}
}
I didn't implement the delegate properly yet, but that not the the point. The print("buttonTapped") won't even show up. The method just doesn't get called, I tested this using breakpoints.
Does anyone know what is causing this problem
I am trying to have two non-editable UITextField display name and age. I have an Edit UIBarButtonItem in my Navigation Bar that I want to be able to trigger the UITextField to be editable when that button is pressed.
In my Interface Builder, I have the User Interaction Enabled option unchecked for the two UITextFields. Do I need to add ageText.UserInteractionEnabled = true? I'm at a loss here.
class UserProfileVC: UIViewController,
UITextFieldDelegate, UINavigationControllerDelegate {
#IBOutlet weak var infoBorder: UILabel!
#IBOutlet weak var nameText: UITextField!
#IBOutlet weak var ageText: UITextField!
var textFields:[UITextField] = []
#IBAction func editButton(sender: UIBarButtonItem) {
nameText.becomeFirstResponder()
ageText.becomeFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
var currentTextField = textFields[0]
if (currentTextField == textField) {
currentTextField = textFields[1]
currentTextField.becomeFirstResponder()
} else {
currentTextField.resignFirstResponder()
}
return true
}
}
Yes, you want to do
nameText.userInteractionEnabled = true
ageText.userInteractionEnabled = true
and possibly
nameText.becomeFirstResponder()
when the edit button gets pressed the first time. You will probably also want to change the "Edit" button do a "Done" button. When the user presses the done button, you'll want to make sure you disable user interaction and resign first responder for both text fields.
You should have a Bool Variable. Set it to 'false' then when the button is pressed you toggle it to 'true'. Depending on its state just run to different methods which essentially allows you to edit or not.
Something like this:
var editTextFieldToggle: Bool = false
#IBoutlet var textFieldToggle: UIButton!
#IBAction func textFieldToggle_Action(sender: UIButton){
editTextFieldToggle = !editTextFieldToggle //switches button ON/OFF
if editTextFieldToggle == true {
textFieldActive()
} else {
textFieldDeactive()
}
}
textFieldActive(){
//Turn things ON
nameText.enabled == true
ageText.enabled == true
}
textFieldDeactive(){ //Add anything else
//Turn things OFF
nameText.enabled == false
ageText.enabled == false
}
All I want to do is keep a button hidden until another button is pressed.
For example, Button 1 is visible, but Button 2 isn't. When I press Button 1, I need Button 2 to appear.
Also, I am programming in Xcode 6 using Swift.
Thanks in advance!
The sample code for hiding a button in Swift:
import UIKit
class ViewController: UIViewController {
// Create outlet for both the button
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//Set button2 hidden at start
button2.hidden = true
}
//Here is the action when you press button1 which is visible
#IBAction func button1(sender: AnyObject) {
//Make button2 Visible
button2.hidden = false
}
}
May be this can help you.
I've implemented an edit burron for my custom tableviewcontroller without using the default edit button. I have put a UIBarButton item in my storyboard and linked with IBOutlet in my custom class. I've implemented all these simple mechanisms for changing the button title:
class DetailTableViewController: UITableViewController {
var selectedMedicine: NSManagedObject?
var edit: Bool = false
#IBOutlet var editButton: UIBarButtonItem
#IBOutlet var nameTextField: UITextField
#IBOutlet var noteTextView: UITextView
#IBAction func enableEditing(sender: AnyObject) {
if !edit
{
println("editing")
self.edit = true
self.navigationItem.rightBarButtonItem.title = "Done"
}
else
{
self.edit = false
self.navigationItem.rightBarButtonItem.title = "Edit"
}
}
The title changes but there's something strange in the transition from Edit to Done because it's a little jerky.
I've found another one with my same problem but no one answered him.
You can look at this video for undertsand the bad animation i mean video
Instead of manually setting the bar button title and tracking state, you can use the built-in UIViewController.editButtonItem and UIViewController.editing property:
func viewWillAppear(animated:Bool) {
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
#IBAction func enableEditing(sender: AnyObject) {
self.editing = !self.editing
}