2011-04-04 5 views
9

मैं अटक गया हूं :(
मेरे आवेदन में मुझे हर बार एक नई स्थिति में अपडेट होने पर CLLocationManager से एक अपडेट की आवश्यकता होती है। मैं XIB/NIB फ़ाइलों का उपयोग नहीं कर रहा हूं, मैंने जो कुछ भी कोड किया है, मैंने कोडेटिक रूप से किया है। :
एक्सआईबी फाइलों के बिना प्रोग्रामिक रूप से एक्सकोड में यूआईएलएबल को कैसे अपडेट करें?


@interface TestViewController : UIViewController 
    UILabel* theLabel; 

@property (nonatomic, copy) UILabel* theLabel; 

@end 

मीटर


... 

-(void)loadView{ 
    .... 
    UILabel* theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)]; 
    theLabel.text = @"this is some text"; 

    [self.view addSubView:theLabel]; 
    [theLabel release]; // even if this gets moved to the dealloc method, it changes nothing... 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"Location: %@", [newLocation description]); 

    // THIS DOES NOTHING TO CHANGE TEXT FOR ME... HELP?? 
    [self.view.theLabel setText:[NSString stringWithFormat: @"Your Location is: %@", [newLocation description]]]; 

    // THIS DOES NOTHING EITHER ?!?!?!? 
    self.view.theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]]; 

} 
... 

कोई भी विचार, या मदद

(यह सब हाथ जाम किया गया था तो कृपया मुझे माफ़ कर दो अगर यह थोड़े गले लगते हैं) यदि आवश्यक हो तो मैं अधिक जानकारी प्रदान कर सकता हूं।

+0

क्या एनएसएलओजी सही स्थान दिखाता है? – GorillaPatch

+0

क्या आपका मतलब व्यूडिडलोड नहीं था? –

+0

@Radek मैंने इसे loadView में रखा है, क्या UILabel को डीडलोड में देखा जाना चाहिए? क्या यह ओवरराइड बिंदु है? और यह UILabel mutable –

उत्तर

16

आपकी लोड व्यू विधि गलत है। आप इंस्टेंस वैरिएबल को ठीक से सेट नहीं करते हैं बल्कि इसके बजाय आप एक नया स्थानीय चर उत्पन्न करते हैं। UILabel * और को छोड़कर इसे निम्नलिखित में बदलें इसे जारी न करें क्योंकि आप बाद में टेक्स्ट सेट करने के लिए लेबल के चारों ओर एक संदर्भ रखना चाहते हैं।

-(void)loadView{ 
    .... 
    theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)]; 
    theLabel.text = @"this is some text"; 

    [self.view addSubView:theLabel]; 
} 

- (void) dealloc { 
    [theLabel release]; 
    [super dealloc]; 
} 

फिर बाद में सीधे इस तरह चर का उपयोग:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"Location: %@", [newLocation description]); 

    theLabel.text = [NSString stringWithFormat: @"Your Location is: %@", [newLocation description]]; 

} 
+0

मुझे अभी भी मूल पाठ "यह कुछ पाठ है" मिलता है, यह कभी अपडेट नहीं होता है, भले ही मैं एक नए स्थान के साथ मुद्रित कंसोल में एनएसएलओजी आउटपुट देख सकूं। –

+0

मैंने जवाब अपडेट किया। मुझे लगता है कि आपके टेलीबेस इंस्टेंस वेरिएबल जो आप स्थान प्रबंधक प्रतिनिधि कॉलबैक में संदर्भित करते हैं, अभी भी शून्य है क्योंकि आपको इसे ठीक से असाइन करना होगा। – GorillaPatch

+0

आप सही थे, इसे स्थानीय var होना आवश्यक था ... आप उपरोक्त कोड को बदल सकते हैं और [theLabel रिलीज] को टिप्पणी के साथ हटा सकते हैं, क्योंकि यह बहुत जल्दी रिलीज़ होगा (यानी उत्तर में एक टाइपो) धन्यवाद, आप रॉक! –

0

आप अपने मीटर फ़ाइल में theLabel synthesizing रहे हैं ...? यदि नहीं, तो आपको विश्वास है कि आपको विश्वास है।

+0

कंपाइलर चेतावनी देगा अगर यह गेटर और सेटर लागू नहीं किया गया है और @ सिंथेसाइज नहीं दिया गया है। – GorillaPatch

+0

कौन सा ओपी अनदेखा करने में कामयाब हो सकता था, संभवतः ... –

संबंधित मुद्दे