2011-11-22 14 views
8

पर ऑब्जेक्ट पास करें मैं अपने मुख्य व्यू क्लास से किसी अन्य वर्ग में अन्य अधिसूचना रिसीवर को ऑब्जेक्ट पास करने का प्रयास कर रहा हूं।एनएसएनोटिफिकेशन सेंटर के साथ अन्य ऑब्जेक्ट

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

देश = [[देश आवंटन] init];

देश हैडर:

@interface Country : NSObject 
{ 
    NSString *name; 
    NSMutableArray *cities; 
} 

@property (nonatomic,retain) NSString *name; 

- (void)addCity:(Cities *)city; 
- (NSArray *)getCities; 
- (int)citiesCount;  
@end 

मैं UserInfo में एक NSDictionary उपयोग कर रहा है NSNotificatios साथ डेटा पास करने के लिए एक रास्ता मिल गया। लेकिन एनएस डिक्शनरी में कनवर्ट करने की बजाए पूरी ऑब्जेक्ट भेजना संभव नहीं है? या इसे स्थानांतरित करने का सबसे अच्छा तरीका क्या है? मैं वस्तुओं को पारित करने के तरीके को समझने की कोशिश कर रहा हूं।

दरअसल मैं अपने ऐप पर इस सरल एनएसएनोटिफिकेशन को काम कर रहा हूं। मुख्य दृश्य नियंत्रक कार्यान्वयन में

NSNotification:

//---Call the next View--- 
DetailViewController *detail = [self.storyboardinstantiateViewControllerWithIdentifier:@"Detail"]; 
[self.navigationController pushViewController:detail animated:YES]; 

//--Transfer Data to2View 
[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil]; 

NSNotification 2View में नियंत्रक कार्यान्वयन:

// Check if MSG is RECEIVE 
- (void)checkMSG:(NSNotification *)note { 

    NSLog(@"Received Notification"); 
} 

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(checkMSG:) 
               name:@"citiesListComplete" object:nil]; 

उत्तर

25

Oooooo, इतने करीब। मुझे लगता है कि आपको समझ में नहीं आता कि NSDictionary क्या है।

पोस्ट अपने इस के साथ अधिसूचना:

Country *country = [[[Country alloc] init] autorelease]; 
//Populate the country object however you want 

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:country forKey:@"Country"]; 

[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil userInfo:dictionary]; 

तो इस तरह देश वस्तु मिलती है:

- (void)checkMSG:(NSNotification *)note { 

    Country *country = [[note userInfo] valueForKey:@"Country"]; 

    NSLog(@"Received Notification - Country = %@", country); 
} 

आप एक NSDictionary में अपने वस्तु बदलने की आवश्यकता नहीं है। इसके बजाय, आपको अपनी ऑब्जेक्ट के साथ NSDictionary भेजने की आवश्यकता है। यह आपको के साथ NSDictionary में कुंजियों के आधार पर बहुत सारी जानकारी भेजने की अनुमति देता है।

+0

धन्यवाद इस शब्दकोश पढ़ सकते हैं! एक जादू की तरह काम करता है। मैंने एनएस डिक्शनरी की अवधारणा को गलत समझा लेकिन इसे और स्पष्ट बताया। –

+0

धन्यवाद, दोस्त! मैं गलत विधि का उपयोग कर रहा था और मुझे पूरी तरह अधिसूचना मिल रही थी लेकिन उपयोगकर्ता इन्फॉर्फ़ नहीं। चीयर्स। – Felipe

4

स्विफ्ट के लिए आप उदाहरण

NSNotificationCenter.defaultCenter().postNotificationName("OrderCancelled", object: nil, userInfo: ["success":true]) 

के लिए नीचे दिए गए कोड

NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?, userInfo aUserInfo: [NSObject : AnyObject]?) 

उपयोग करने के साथ शब्दकोश पारित और से

func updated(notification: NSNotification){ 

     notification.userInfo?["success"] as! Bool 
    } 
संबंधित मुद्दे