-4

Here is my code:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var maximumNumber: UITextField!
    @IBAction func playButtonPressed(_ sender: Any) {
        maximumNumber.isHidden = true
        guessTextField.isHidden = false
    }
    @IBOutlet weak var guessTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        guessTextField.isHidden = true
        maximumNumber.keyboardType = .numberPad
        guessTextField.keyboardType = .numberPad

        func textField(_ textField: UITextField,
                       shouldChangeCharactersIn range: NSRange,
                       replacementString string: String) -> Bool {

            // Create an `NSCharacterSet` set which includes everything *but* the digits
            let inverseSet = NSCharacterSet(charactersIn:"0123456789").inverted

            // At every character in this "inverseSet" contained in the string,
            // split the string up into components which exclude the characters
            // in this inverse set
            let components = string.components(separatedBy: inverseSet)

            // Rejoin these components
            let filtered = components.joined(separator: "")  // use join("", components) if you are using Swift 1.2

            // If the original string is equal to the filtered string, i.e. if no
            // inverse characters were present to be eliminated, the input is valid
            // and the statement returns true; else it returns false
            return string == filtered  
        }
    }
}

I want the text fields maximumNumber and guessTextField allow only integers. I've tried using other functions and delegates I've found here, but they fail to work or I get compiler errors.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
JAwesomeness
  • 1
  • 1
  • 4
  • _I've tried using other functions and delegates I've found here, but they fail to work or I get compiler errors_ But in the code _you_ showed, you are doing _nothing_ to limit what the user can enter into this text field. Nothing will come of nothing. Show some code. – matt Jan 08 '17 at 01:32
  • Since they don't work I've deleted them. – JAwesomeness Jan 08 '17 at 01:36
  • But in that case you are just asking for someone to write your entire code for you from scratch. That's not really appropriate. – matt Jan 08 '17 at 01:38
  • http://stackoverflow.com/questions/12944789/allow-only-numbers-for-uitextfield-input – JAwesomeness Jan 08 '17 at 01:40
  • http://stackoverflow.com/questions/30973044/how-to-restrict-uitextfield-to-take-only-numbers-in-swift – JAwesomeness Jan 08 '17 at 01:41
  • Why are you just linking other questions? Are you saying that all of those methods didn't work? – Benjamin Lowry Jan 08 '17 at 01:42
  • Yes. Or I've been putting them in the wrong spot. I consider since I have different variables and all that something was wrong. I tried changing the variable names but it still did nothing when I built it. – JAwesomeness Jan 08 '17 at 01:44
  • But how will we know if you've been putting them in the wrong spot or what you might be doing wrong if you don't show what you're doing? I repeat: it's not our job to write your code from scratch. And your own links prove that this question should just be shut down as a duplicate. If you don't want that to happen, you need to be more proactive at your end. Stack Overflow is not a programmer-for-free-hire service: _you_ have to be the programmer. – matt Jan 08 '17 at 01:48
  • I am fairly sure that many of the methods included in your two links should work for you. I don't see anything special about your viewController, so this is probably just a duplicate question. I would pay particular attention to [this answer](http://stackoverflow.com/a/31812151/5179910) as I believe it is very straightforward. If you follow its steps then your program should work. – Benjamin Lowry Jan 08 '17 at 01:50
  • I don't know how to "set up a protocol" or anything like that. – JAwesomeness Jan 08 '17 at 01:55
  • this was the only method that gave no errors, yet it did nothing on running it. http://pastebin.com/RKQ9h70D – JAwesomeness Jan 08 '17 at 02:14
  • 1
    We are not going to go running off to look at some pastebin. Show your code _here_ as part of your question. – matt Jan 08 '17 at 02:17
  • Do you want to restrict the number to some maximum integer also ?? or you want user to enter only interger? – Moin Shirazi Jan 08 '17 at 04:04

5 Answers5

2

This will help - Swift 3

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {
        return false
    }
    return true
}
Barath
  • 1,258
  • 15
  • 18
0

It looks like you have your textfield(shouldChangeCharactersIn:replacementString:) method nested inside viewDidLoad.

Delegate methods like that must be at the top level of the delegate's class or they won't get called.

Move your method to the top level of your view controller, and then add a breakpoint and make sure it's being called.

Duncan C
  • 115,063
  • 19
  • 151
  • 241
0

swift 3

textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool   

above is method which should be in viewcontroller but outside any method, it should not be in viewDidLoad() method

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField==yourTextFieldOutlet {
                if(CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: yourTextFieldOutlet.text!))){
//if numbers only, then your code here
                }
                else{
                showAlert(title: "Error",message: "Enter Number only",type: "failure")
                }
            }
    return true
    }
ArgaPK
  • 419
  • 1
  • 8
  • 21
0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == ageTextField {
        guard NSCharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
            return false
        }
    }
    return true
}
0
extension JobCreationViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        guard NSCharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
            return false
        }
        return true
    }
}
Vincent Mungai
  • 142
  • 2
  • 3