在Swift中的NSTextfield中设置最大字符数(一个) [英] Set maximum characters (to one) in a NSTextfield in Swift

查看:212
本文介绍了在Swift中的NSTextfield中设置最大字符数(一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中将多个NSTextfields(OSX可可应用程序,不是iOS)中的最大字符数设置为1?

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

请说明如何执行此操作,因为在OSX应用程序开发方面我是一个完全的新手,因此我不理解使用NSFormatter之类的简短答案。 ,因为我不知道它是什么以及如何实现它。类似于示例

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

推荐答案

您无需限制用户输入的字符,只需查看输入的第一个字符即可。实际上,这样做可能更好,因为您将始终必须处理可能的用户错误。如果您愿意,可以通过获取character.count来发出警报,告知他们输入了太多信息。如果他们根本不回答,则可能需要警报。如果您使用1个NSTextField和一个按钮设置一个情节提要板并将它们连接起来,则下面的代码将按原样工作。如果您有多个文本字段(例如,多项选择测试),则以相同的方式设置所有文本字段。

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弱变量secondLetter:NSTextField!

@IBOutlet weak var secondLetter: NSTextField!

@IBOutlet弱变量secondLetter:NSTextField!

@IBOutlet weak var thirdLetter: NSTextField!

    @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()
    }
}

这篇关于在Swift中的NSTextfield中设置最大字符数(一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆