Disable text field interaction with segue

in a storyboard-based application, you will often want to do a little preparation before navigation
view controller life cycle
table view controller
viewcontroller
how to open view controller scene in xcode
ios view controller tutorial
how to connect view controllers in xcode
swift how to use table view

I want to make textfield's user interaction disabled when the user tapped on tableview's row and the segue is applied.Where did I something wrong ? I am getting "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" error when I select row at tableview.

here is my ViewController class and its necessary functions

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedTrip = dateArray[indexPath.row]
    performSegue(withIdentifier: "toDetailsVC", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toDetailsVC" {
        let destinationVC = segue.destination as! detailsViewController
        destinationVC.kmField.isUserInteractionEnabled = false
    }
}

and kmField in detailsVC is like

@IBOutlet weak var kmField: UITextField!

The view has not yet been loaded from the storyboard when prepareForSegue is called, therefore your attempt to access the implicitly unwrapped optional kmField hits a nil.

It is generally a bad idea to access the internals of a class, such as a text field, directly from outside.

I would suggest you add a boolean property, say isEditable, that you can then use in viewDidLoad to set up your view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationVC = segue.destination as? DetailsViewController
        destinationVC.isEditable = false
    }
}

By the way, by convention your view controller class should be DetailsViewController

class DetailsViewController {

    var isEditable = true
    @IBOutlet private weak var kmField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.kmField.isUserInteractionEnabled = self.isEditable
    }

Disable text field interaction with segue, I want to make textfield's user interaction disabled when the user tapped on tableview's row and the segue is applied.Where did I something wrong ? I am getting  If the user touches a done button and the text in a text field does not pass validation how do I stop the segue associated with button from executing? TIA This site uses cookies for analytics, personalized content and ads.

Hello @efedoganay you need to understand the life cycle of viewcontroller. Function Prepare not initialize views property on screen, so you need to wait for sometime and make changes in other controller DetailsViewController viewdidLoad Method.Or as @paul suggested set value in some variable and on the base of that set interaction property.

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    kmfield.isUserInteractionEnabled = false
}

Disable text field interaction with segue, I want to make textfield's user interaction disabled when the user tapped on tableview's row and the segue is applied.Where did I something wrong ? I am getting  The disabled attribute is a boolean attribute. When present, it specifies that the <input> element should be disabled. A disabled input element is unusable and un-clickable. The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.).

It is not possible to access internals of class like Textfields,buttons and labels which are taking reference from xib. until viewLoad those values are nil.

so better to take a boolean variable and make it false in tableview didselectrow.

based on that condition you can enable and disable the textfeild.

var isUserInteractionEnable = false

override func viewDidLoad() {
  kmfield.isUserInteractionEnabled = isUserInteractionEnable
}

Hope it helps.

How To Disable A TextField in Flutter · zaiste.net, as bold font style and change the background color. A text field manages the editing of its text with the help of its delegate object. As the user interacts with a text field, the text field notifies its delegate and gives it a chance to control what is happening. You can use the delegate methods to prevent the user from starting or stopping the editing process or to validate text as it is typed.

iOS UITextfield disable editing for typing swift, How do I disable UITextField editing but still accept touch? The disabled attribute is a boolean attribute. When present, it specifies that the element should be disabled. A disabled element is unusable. The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.).

ios How to disable UITextField editing but still accept touch?, How to disable keyboard on specific textField but allow picker view as input Swift​? I embed them to my view controller and access its tableView during segue. One disable user interaction forever and use another textField which will never  A cursor appears in a text field, letting you enter text. Type Your Meals and press Return. This sets the title for the table view controller’s navigation item. Open the Object library. (Choose View > Utilities > Show Object Library.) In the Object library, find a Bar Button Item object.

How to disable keyboard on specific textField but allow picker view , In this lesson, you use navigation controllers and segues to create the navigation flow of the At the end of the lesson, you'll have a complete navigation scheme and interaction flow for the app. A cursor appears in a text field, letting you enter text. To disable the Save button when there's no item name. (1) Enabled and (2) disabled buttons differentiate through color and elevation changes. Disabled state inheritance. Components can inherit a disabled state, such as toggle buttons, selection controls, text fields, icons, grid and list items, cards, chips, and buttons.

Comments
  • You should put a breakpoint in and check to see which value is nil. Your problem sounds like it's before you get to the textField. It would be helpful to post whatever the compiler barfs into the console with your question, as the source of your problem is likely spelled out there.
  • I checked it and compiler sees kmField as nil.
  • Guessing the view's not rendered yet. Perhaps you might want to move the kmField.isUserInteractionEnabled = false to DetailsViewController's viewDidLoad/viewDidAppear and deal with it there instead of trying to manipulate something that's not loaded yet.
  • Did you set this segue id on your storyboard toDetailsVC
  • yes I did that.