Your Second Swift 4 & iOS 11 App - Part 31: | Ray Wenderlich

There are times when you need to respond to special kinds of events. These are called control events and you'll learn about them in this video.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4284-your-second-swift-4-ios-11-app/lessons/31

I have a question, if i were to use swift 3.0, how can i use the Range(range , in : oldText) in shouldchangecharacterInRange for textField?

1 Like

This series was made on Xcode 9 and Swift 4. Swift 4 has provided range bridging. You’ll have to do some digging to translate to Swift 3. You can also check out the older version of this course which was made in Swift 3 over here:

https://videos.raywenderlich.com/courses/64-beginning-ios-10-part-2-checklists/lessons/1

Hi, I have done everything like in the video, but the doneBarButton is still enabled. Only in keyboard is disabled when textField is empty, but the actual button it’s not changing.

@vegetarianzombie @bdmoakley Can you please help with this one when you get a chance? Thank you - much appreciated! :]

It’s hard to diagnose an issue without any code. Would you paste in the code so I can take a look?

@IBAction func done() {
if let itemToEdit = itemToEdit {
itemToEdit.text = textField.text!
delegate?.itemDetailViewController(self, didFinishEditing: itemToEdit)
} else {
let item = ChecklistItem()
item.text = textField.text!
item.checked = false
delegate?.itemDetailViewController(self, didFinishAdding: item)
}
}

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    return nil
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    let oldText = textField.text!
    let stringRange = Range(range, in:oldText)
    let newText = oldText.replacingCharacters(in: stringRange!, with: string)
    if newText.isEmpty {
        doneBarButton.isEnabled = false
    } else {
        doneBarButton.isEnabled = true
    }
    return true
}

}

I’m also having the issue of the done button not being disabled when the text field is empty.

If the button is still being disabled, then there is still a string present in newText. My suggestion: before the isEmpty check, print out the results of newText. My guess is that it contains a space character. So it would look something like this: " "

Play around with it and let me know what you discover.

This topic was automatically closed after 166 days. New replies are no longer allowed.

Question about this lesson:

    if newText.isEmpty {
        doneBarButton.isEnabled = false
    }else {
        doneBarButton.isEnabled = true
    }

I get the error: Value of type ‘UINavigationItem’ has no member ‘isEnabled’

I think I have the outlet configured correctly:

@IBOutlet var doneBarButton: UINavigationItem!  

Any ideas?

I solved this problem by adding to the function viewWillAppear this statement:
done.isEnabled = false
Is it ok?

sorry for interrupting again, but I didn’t understand for what do we need this piece of code:
let oldText = textField.text!
let stringRange = Range(range, in: oldText)
let newText = oldText.replacingCharacters(in: stringRange!, with: string)
In the video you said that it allows user to copy and paste text, but what actually this code does?
Thanks

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

It wasn’t really explained in the video. Basically, this code checks to see if there is any text that the user is typing into the text field and compares against the old values. If it’s empty, then you know that the user may have selected all, and deleted everything or just backspaced until everything was gone.