Wednesday, September 22, 2010

iPhone SDK : viewWillAppear

iPhone View management is really interesting. For instance, if you have a tab bar controller application and you have multiple tab bar items.


iPhone on launch of the app will automatically parse & compile all the ViewDidLoad methods across all the tab bar items. 


However, based on certain user input in one tab bar item, you might want to change the content of the another tab item, iPhone by default does not allow it. Because the ViewDidLoad method fires only once and if your code to change the content is in the ViewDidLoad method, it will never get fired till the user closes and re-opens the app. With the new iPhone 4.0, all the applications remain in the background. So even if the user closes the application by pressing the home button. The view does not get refreshed at all. More about that at a later point.




You can always force the view to refresh itself by adding your code to the method
-(void)viewWillAppear:(bool) animated
{
/*write your code to refresh your content*/
}


viewWillAppear is guaranteed to fire everytime, the view appears. 


Example: In one of my apps, I had an option to change certain items in the configuration view, all of my other views in the tab bar controller would not get refreshed to read the changed items in the configuration view unless i had a button to refresh the view content. Putting my refresh code in viewWillAppear did the trick. Now all my views get refreshed in real Time.


For those of you, who want to see this in action, download my app at
http://itunes.apple.com/us/app/homebuilders/id389989024?mt=8


Have fun folks.

No comments: