2016-07-05 5 views
0

학생들이 특정 텍스트 필드에서 등급을 입력하는 앱을 만들고 있습니다. 0에서 100까지의 숫자를 입력하기를 원합니다. 텍스트 필드에 최대 NUMBER 값을 설정하는 방법에 대해 궁금합니다.텍스트 필드의 최대 숫자 값 설정

나는 어떤 유형의 행동을 취한다고 가정하고 있지만 어떤 행동 유형을 사용해야하는지 또는 최대 값을 설정하는 코드를 모르겠습니다.

도움이 될 것입니다. :) !!

+0

0에서 100까지의 값을 가진 UIPickerView를 사용할 수 있습니다. – vacawama

+0

@vacawama 저는 신속하고 xcode에 익숙하지만 실제로 UIPickerView를 사용하는 방법을 모르겠습니다. –

답변

1

제한된 수의 고정 답변을 표시하려면 UCPickerView를 사용할 수 있습니다 : . 당신이 그 (것)들을 텍스트 상자에 입력해야 할 경우, 당신은 지금처럼 textFieldDidEndEditing 방법의 입력 유효성을 검사 할 수 있습니다 :

func textFieldDidEndEditing(textField: UITextField) { 
    if textField.text.toInt() < 0 || textfield.text.toInt() > 100 { 
     //Here you can present an alert, change the input or clear the field. 
    } 
} 
0

대안은 확인하는 것입니다 점수는 0 때 부정과 100 사용자가 더 뭔가를 입력하면 (100)보다 이것은 당신이 72.5 같은 소수 점수를 입력 할 수 있습니다 :

class ViewController: UIViewController, UITextFieldDelegate { 
    @IBOutlet weak var textField: UITextField! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.textField.delegate = self 
    } 

    func textFieldShouldEndEditing(textField: UITextField) -> Bool { 
     // Make sure the user entered a number 
     guard let score = Double(textField.text!) else { 
      let alert = UIAlertController(title: "Invalid Score", message: "Score must be a number from 0 to 100", preferredStyle: .Alert) 
      alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
      self.presentViewController(alert, animated: true, completion: nil) 
      return false 
     } 

     if score < 0 { 
      textField.text = "0" 
     } else if score > 100 { 
      textField.text = "100" 
     } 
     return true 
    } 
} 
0
internal func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 



    if ((self.inputTextField?.text)!.characters.count > 10 && range.length == 0) 

    { 
     return false 
     //return false // return NO to not change text 
    }else{ 

     return true 

    } 

} 

참고 : 가져 오기 텍스트 필드 UITextFieldDelegat.

관련 문제