Set maximum characters (to one) in a NSTextfield in Swift

↘锁芯ラ 提交于 2019-12-08 03:40:09

问题


How do i set the maximum amount of characters in multiple NSTextfields (OSX cocoa app, NOT iOS) to one in Swift?

Please explain how to do it, because I'm a complete newbie when it comes to OSX app development and therefore I don't understand short answers like "Use NSFormatter", because I have no idea what it is and how to implement it. Like Examples


回答1:


You don't need to limit the characters a user will enter just only look at the first character entered. In fact, it is probably better since you will always have to handle possible user errors. If you want to you can issue an alert that they entered too many by getting the characters.count. You might want an alert if they don't answer at all. The code below will work as is if you set up a storyboard with 1 NSTextField and one button and connect them. If you have more than one textfield, i.e. like a multiple choice test, just set up all the text fields the same way.

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var firstLetter: NSTextField!

Add as many text fields as you need:

@IBOutlet weak var secondLetter: NSTextField!

@IBOutlet weak var thirdLetter: NSTextField!

etc.

    @IBAction func button(sender: AnyObject) {

        var firstEntry = firstLetter!.stringValue

        var index1 =  firstEntry.startIndex


        if firstEntry.characters.count > 1 {

            runMyAlert("Bad USER! ONLY ONE Character!")
        }

        if firstEntry == "" {  //left it blank

            runMyAlert("You need to enter at least one character!")
            exit(0) //or you'll crash on next line
        }

        var nameLetter1:Character = firstEntry[index1]

        print( "First Letter == \(nameLetter1) ")

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    func runMyAlert( alertMessage: String){

        var myWindow = NSWindow.self
        let alert = NSAlert()
        alert.messageText = "ERROR ERROR ERROR"
        alert.addButtonWithTitle("OK")
        alert.informativeText = alertMessage
        alert.runModal()
    }
}



回答2:


There's no built-in way to simply set the maximum, I think because you need to decide what behavior you want. For example, if there's already one character in the field, and the user enters a second character, what should happen? Should the 2nd character be ignored? Replace the first? Beep?

In any case, you can get whatever behavior you want using the NSText Delegate methods. Make your view controller (or whatever object has the logic) a delegate of the text field, and implement the various delegate method(s) to do what you need.

Again, the exact behavior is up to you, but if I were implementing this, I might be inclined to make the text field always use the last character entered (such that, if one character is already present, pressing a second replaces the first). To do that, you'd want to override textDidChange to look at the value of the text field, and modify it if appropriate. Something like:

class ViewController: NSViewController, NSTextFieldDelegate {

    @IBOutlet weak var textField: NSTextField!

    override func controlTextDidChange(obj: NSNotification) {
        if self.textField.stringValue.characters.count > 1 {
            self.textField.stringValue = String(self.textField.stringValue.characters.last!)
        }
    }
}


来源:https://stackoverflow.com/questions/35589503/set-maximum-characters-to-one-in-a-nstextfield-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!