2012-07-14 43 views
42

के लिए पाठ परिवर्तन अधिसूचना एनएसटीक्स्टफिल्ड में संग्रहीत स्ट्रिंग पर परिवर्तनों का निरीक्षण करने के लिए एनएसटीक्स्टफिल्ड पर How to observe the value of an NSTextField पर इस प्रश्न के उत्तर से कोड का उपयोग करना चाहूंगा।एनएसटीक्स्टफाल्ड

[[NSNotificationCenter defaultCenter] 
    addObserverForName:NSTextViewDidChangeSelectionNotification 
    object:self.textView 
    queue:[NSOperationQueue mainQueue] 
    usingBlock:^(NSNotification *note){ 
    NSLog(@"Text: %@", self.textView.textStorage.string); 
}]; 

यहां उपयोग की जाने वाली कक्षा एक NSTextView है। मुझे NSTextViewDidChangeSelectionNotification के बजाय उपयोग करने के लिए NSTextField में अधिसूचना नहीं मिल रही है।

क्या एनएसटीक्स्टफिल्ड में कोई सूचना उपलब्ध है जिसका उपयोग इस मामले में किया जा सकता है?

+1

आप स्पष्ट कर सकते हैं तो आपको सूचित किया करने के लिए देख रहे हैं कि क्या जब पाठ फ़ील्ड के stringValue परिवर्तन, या जब के साथ जुड़ा हुआ जवाब सौदों के रूप में चयनित पाठ में परिवर्तन ()? – NSGod

उत्तर

89

यदि आप सिर्फ यह पता लगाना चाहते हैं कि टेक्स्ट फ़ील्ड का मूल्य बदल गया है, तो आप प्रतिनिधि विधि का उपयोग कर सकते हैं जो NSTextFieldNSControl से विरासत प्राप्त करता है।

बस अपने नियंत्रक वर्ग के लिए निब फ़ाइल में NSTextField की delegate आउटलेट कनेक्ट करते हैं, और कुछ इस तरह लागू:

- (void)controlTextDidChange:(NSNotification *)notification { 
    NSTextField *textField = [notification object]; 
    NSLog(@"controlTextDidChange: stringValue == %@", [textField stringValue]); 
} 

आप NSTextField प्रोग्राम के रूप में बना रहे हैं, तो आप उपयोग कर सकते हैं NSTextField के setDelegate:

NSTextField *textField = [[[NSTextField alloc] initWithFrame:someRect] autorelease]; 
[textField setDelegate:self]; // or whatever object you want 

प्रतिनिधिमंडल कोको भर में इस्तेमाल मौलिक डिजाइन पैटर्न में से एक है: निर्माण के बाद विधि प्रतिनिधि निर्दिष्ट करने के लिए। संक्षेप में, यह आपको उस अतिरिक्त व्यवहार को जोड़ने के लिए ऑब्जेक्ट को उप-वर्ग में शामिल जटिलता के बिना मानक ऑब्जेक्ट्स (इस मामले में, उपयोगकर्ता इंटरफ़ेस ऑब्जेक्ट्स) के व्यवहार को आसानी से अनुकूलित करने की अनुमति देता है। उदाहरण के लिए, टेक्स्टफील्ड में टेक्स्ट बदलने पर एक और निम्न-स्तरीय तरीका यह हो सकता है कि आप अपना खुद का कस्टम NSTextField सबक्लास बना सकते हैं जिसमें keyDown: विधि NSResponder से विरासत को ओवरराइड कर सकती है। हालांकि, इस तरह के उपclassing मुश्किल है क्योंकि यह आवश्यक है कि आप वस्तु के विरासत पदानुक्रम का एक अंतरंग ज्ञान है। अधिक जानकारी के लिए, निश्चित रूप से बाहर की जाँच करें:

Cocoa Fundamentals Guide: Delegates and Data Sources

के बारे में क्या id <NSTextFieldDelegate> का अर्थ है: यह एक सामान्य वस्तु (id) कि <NSTextFieldDelegate> प्रोटोकॉल के अनुरूप के रूप में घोषित किया है। प्रोटोकॉल पर अधिक जानकारी के लिए, The Objective-C Programming Language: Protocols देखें।

नमूना GitHub परियोजना पर: https://github.com/NSGod/MDControlTextDidChange

+0

मैं आईबी के साथ NSTextField नहीं बना रहा हूं, लेकिन प्रोग्रामेटिक रूप से। इस के लिए एक समकक्ष विधि है? – alecail

+0

प्रोग्रामेटिक सृजन के लिए अद्यतन उत्तर – NSGod

+0

प्रतिनिधि मेरे लिए नए हैं। मुझे नहीं पता कि वास्तव में 'id ' क्या है और 'setTelegate' विधि को 'controlTextDidChange' विधि' कहां रखना है के लिए, लेकिन मुझे जल्द ही पता चल जाएगा। – alecail

0

मेरा मानना ​​है कि आप field editor पर पढ़ना चाहते हैं जो अनिवार्य रूप से एक (छुपा) NSTextView है जो किसी दिए गए विंडो में सभी NSTextField s पर पाठ इनपुट को संभालता है। "Using Delegation and Notification With the Field Editor" पर अनुभाग आपको सही दिशा में इंगित करना चाहिए।

+1

टूटे हुए लिंक :(यहां आज़माएं: https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextFieldsAndViews/TextFieldsAndViews.html – ericpeters0n

0

स्विफ्ट में यह

public override func controlTextDidChange(_ obj: Notification) { 


} 
0

Xcode 9.2 है। स्विफ्ट 4.0.3 के साथ।

इस कार्यान्वयन के लिए एनएसटीक्स्टफाल्ड को इंटरफ़ेस बिल्डर के माध्यम से कनेक्ट किया जाना चाहिए।

import Cocoa 

@objc public class MyWindowController: NSWindowController, NSTextFieldDelegate { 

@IBOutlet weak var myTextField: NSTextField! 

// MARK: - ViewController lifecycle - 

override public func windowDidLoad() { 
    super.windowDidLoad() 
    myTextField.delegate = self 
} 

// MARK: - NSTextFieldDelegate - 

public override func controlTextDidChange(_ obj: Notification) { 
    // check the identifier to be sure you have the correct textfield if more are used 
    if let textField = obj.object as? NSTextField, self.myTextField.identifier == textField.identifier { 
     print("\n\nMy own textField = \(self.myTextField)\nNotification textfield = \(textField)") 
     print("\nChanged text = \(textField.stringValue)\n") 
    } 
} 

}

कंसोल आउटपुट:

My own textField = Optional(<myApp.NSTextField 0x103f1e720>) 
Notification textfield = <myApp.NSTextField: 0x103f1e720> 

Changed text = asdasdasddsada