2011-06-27 18 views
6

की ओर जाता है मेरे पास NSNotification ऑब्जेक्ट के साथ एक अजीब व्यवहार है।
मेरे ऐप में एक नेविगेशन नियंत्रक है, पहला दृश्य एक टेबल व्यू है और दूसरा दृश्य केवल एक व्यू कंट्रोलर है जो चयनित सेल का डेटा दिखाता है।
तो इस डेटा में नियंत्रक देखें जब मैं एक बटन दबाता हूं तो मैं एक अधिसूचना भेजता हूं। अधिसूचना पहले भी काम करती है।एनएसएनोटिफिकेशन सेगमेंटेशन गलती

लेकिन जब मैं तालिका दृश्य पर वापस जाता हूं और डेटा दृश्य नियंत्रक को फिर से ढेर पर धक्का देता हूं और अधिसूचना के साथ बटन को स्पर्श करता हूं, तो संपूर्ण ऐप कोई त्रुटि लॉग के साथ क्रैश हो जाता है।

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"toggleNoteView" object:nil]; 

समारोह जहाँ मैं सूचना भेज:

- (id)init { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(toggleNoteView:) 
              name:@"toggleNoteView" object:nil]; 
    ... 
} 

- (void) toggleNoteView:(NSNotification *)notif { 

    takingNotes = !takingNotes; 
} 

संपादित करें::

- (IBAction) toggleNoteView: (id) sender 
{ 
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"toggleNoteView" object:nil]; 
} 

यह रिसीवर है अब मैं कुछ त्रुटि मिली थी
Xcode केवल इस लाइन पर प्रकाश डाला गया लॉग।

2011-06-27 23:05:05.957 L3T[3228:707] -[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0 
2011-06-27 23:05:06.075 L3T[3228:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0' 
*** Call stack at first throw: 
(
0 CoreFoundation      0x3634f64f __exceptionPreprocess + 114 
1 libobjc.A.dylib      0x370a2c5d objc_exception_throw + 24 
2 CoreFoundation      0x363531bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 
3 CoreFoundation      0x36352649 ___forwarding___ + 508 
4 CoreFoundation      0x362c9180 _CF_forwarding_prep_0 + 48 
5 Foundation       0x35c45183 _nsnote_callback + 142 
6 CoreFoundation      0x3631e20f __CFXNotificationPost_old + 402 
7 CoreFoundation      0x362b8eeb _CFXNotificationPostNotification + 118 
8 Foundation       0x35c425d3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 70 
9 Foundation       0x35c441c1 -[NSNotificationCenter postNotificationName:object:] + 24 
10 L3T         0x0003d17f -[Container toggleNoteView:] + 338 
11 CoreFoundation      0x362bf571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24 

उत्तर

8

जब आप दृश्य को अनलोड करते हैं तो पर्यवेक्षक को हटाने के लिए मत भूलना। असल में क्या हो रहा है जब आप एक गैर-मौजूदा दृश्य में अधिसूचना पोस्ट करते हैं तो यह चयनकर्ता नहीं चला सकता है, इस प्रकार आपके ऐप को क्रैश कर सकता है।

-(void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [super dealloc]; 
} 
3

अपने विवरण से, यह हर बार जब आप एक दृश्य नियंत्रक धक्का तरह, आप दृश्य नियंत्रक का एक नया उदाहरण बना रहे हैं कि ऐसा करने के लिए लगता है।

यदि ऐसा है, तो आपको पहले यह सुनिश्चित करना होगा कि जब आप टेबल व्यू पर वापस जाएं तो आप उस व्यू कंट्रोलर को लीक नहीं कर रहे हैं।

फिर, उस ऑब्जेक्ट की डेलोक विधि में, इसे अधिसूचनाओं से सदस्यता छोड़ दें।

-(void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    //other deallocation code 
    [super dealloc]; 
} 
संबंधित मुद्दे