Check textfield in alertview

I am more or less new to iOS and there is a lot of fundamental stuff I am missing. I am following your HitList tutorial, and I would like to check wheter my textfield in the alertview is empty or not. So if the field is empty the save-button should be dissabled. How do I do that?

thanks,
Joachim

You would need to start off with it being disabled. Mark your UITextField’s delegate to be the view controller, and implement this method:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let oldText = textField.text!
    let swiftRange = Range(range, in: oldText)!
    let newString = oldText.replacingCharacters(in: swiftRange, with: string)

    saveButton.enabled = !newString.isEmpty

    return true;

Oh sorry, you said in an alert view, which is different. See here: this post

This code in Objective-C but it should be the same idea in swift

NSString *text =  [[alertView textFieldAtIndex:0] text];

edited:
Sorry @gargoyle didn’t see your link

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