2017-12-25 44 views
0

텍스트 필드 텍스트가 어떤 방식 으로든 편집되면 함수를 호출하려고합니다.텍스트 필드에 대한 호출 기능 변경

인스턴트 메신저 및 코드 벽에 익숙하지 않아서 필자가 이해하는 데 도움이되지 않고 그 답을 찾을 수있었습니다.

텍스트 필드를 Ctrl 키를 누른 상태에서 '편집을 시작 했음'또는 그와 비슷한 이름으로 보낸 작업을 보여 주었지만 '조치'라는 동작 만 보냈습니다. 나는 설명이 필요하다.

편집 : 이것은 MacOS 응용 프로그램 용이며 UIKit는 작동하지 않습니다.

import Cocoa 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate { 

    @IBOutlet weak var window: NSWindow! 
    @IBOutlet weak var msgBox: NSTextField! 
    @IBOutlet weak var keyBox: NSTextField! 
    @IBOutlet weak var encBtn: NSButton! 
    @IBOutlet weak var decBtn: NSButton! 
    override func controlTextDidChange(_ obj: Notification) { 
     //makeKey() 
     keyBox.stringValue = "test" 
    } 

    override func controlTextDidBeginEditing(_ obj: Notification) { 
     print("Did begin editing...") 
    } 

    func applicationDidFinishLaunching(_ aNotification: Notification) { 
     // Insert code here to initialize your application 
    } 

    func applicationWillTerminate(_ aNotification: Notification) { 
     // Insert code here to tear down your application 
    } 

    func makeKey() { 
     keyBox.stringValue = "test" 
    } 
} 

답변

0

macOS의 경우 iOS와 비슷하지만 NSTextFieldDelegate입니다.

단계는 다음과 같습니다

1) 드래그 앤 드롭 NSTextField 예를 창문에.

2) NSViewController에의 위임을 설정합니다

Setting up delegate

3) 확인하여 ViewController (또는 다른 관리 클래스) NSTextFieldDelegate 구현하고 필요한 텍스트 변화 관련 조치를 구현 :

class ViewController: NSViewController, NSTextFieldDelegate { 

    // Occurs whenever there's any input in the field 
    override func controlTextDidChange(_ obj: Notification) { 
     let textField = obj.object as! NSTextField 
     print("Change occured. \(textField.stringValue)") 
    } 

    // Occurs whenever you input first symbol after focus is here 
    override func controlTextDidBeginEditing(_ obj: Notification) { 
     let textField = obj.object as! NSTextField 
     print("Did begin editing... \(textField.stringValue)") 
    } 

    // Occurs whenever you leave text field (focus lost) 
    override func controlTextDidEndEditing(_ obj: Notification) { 
     let textField = obj.object as! NSTextField 
     print("Ended editing... \(textField.stringValue)") 
    } 
} 
+0

이 코드는 어디에 있습니까? 나는 한 페이지에있는 모든 코드에 익숙합니다 ... – Beejo

+0

이것은 NSTextField 이벤트를 처리하는 ViewController의 코드입니다. – Hexfire

+0

좋아요, 전체 코드를 게시했는데, 잘못 생각 했으므로 (작동하지 않기 때문에) 어쩌면 도움이 될지도 모릅니다. 이것이 내가 어떻게 보일 것 같아요. – Beejo