2012-11-09 9 views
6

संभव डुप्लिकेट:
How to tell when controller has resumed from background?एप्लिकेशन में प्रवेश करने के बाद दृश्य को रीफ्रेश कैसे करें WillEnterForeground?

कैसे ताज़ा करने के लिए देखें उपयोगकर्ता applicationWillEnterForeground में प्रवेश करती है के बाद?

उदाहरण के लिए मैं होमव्यू कंट्रोलर को याद करना चाहता हूं।

मेरे पास होम व्यू कंट्रोलर में फ़ंक्शन है और अपडेट करें और मैं चाहता हूं कि जब उपयोगकर्ता अद्यतन फ़ंक्शन को कॉल करने और तालिका डेटा पुनः लोड करने में प्रवेश करता है।

उत्तर

9

कोई भी वर्ग UIApplicationWillEnterForegroundNotification पर पंजीकरण कर सकता है, और तदनुसार प्रतिक्रिया दे सकता है। यह ऐप प्रतिनिधि के लिए आरक्षित नहीं है, और स्रोत कोड के बेहतर अलगाव के लिए मदद करता है।

+0

क्या आपके पास कुछ उदाहरण लिंक है क्योंकि मैंने कभी उपयोगकर्ता UIAplicationWillEnter नहीं किया है ForegroundNotification? – CroiOS

+3

http://stackoverflow.com/questions/3535907/how-to-tell-when-controller-has-resumed-from-background – Cyrille

+0

उत्कृष्ट, यह काम करता है। आपका बहुत बहुत धन्यवाद। – CroiOS

0

आप अपने ऐप प्रतिनिधि वर्ग में एक संपत्ति घोषित कर सकते हैं जो होमव्यू कंट्रोलर ऑब्जेक्ट को इंगित करेगा। फिर आप अपने अपडेट फ़ंक्शन को एप्लिकेशन WillEnterForeground में कॉल कर सकते हैं।

7

अपने HomeViewController

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(yourUpdateMethodGoesHere:) 
               name:UIApplicationWillEnterForegroundNotification 
               object:nil]; 
} 

// Don't forget to remove the observer in your dealloc method. 
// Otherwise it will stay retained by the [NSNotificationCenter defaultCenter]... 
- (void) dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [super dealloc]; 
} 

के लिए इस तरह की एक viewDidLoad विधि अपना ViewController एक tableViewController अपने भी सीधे पुनः लोड डेटा समारोह कह सकते है:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:[self tableView] 
              selector:@selector(reloadData) 
               name:UIApplicationWillEnterForegroundNotification 
               object:nil]; 

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

या आप एक ब्लॉक इस्तेमाल कर सकते हैं:

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification 
                object:nil 
                queue:[NSOperationQueue mainQueue] 
               usingBlock:^(NSNotification *note) { 
                [[self tableView] reloadData]; 
               }]; 
+0

डेलोक पर पर्यवेक्षक को हटाने के लिए मत भूलना! – Cyrille

+0

वास्तव में सिरिल! – Tieme

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