How to Release Objects initiated in the ViewController - iphone

I am filling some Objects from my View Controller using the following Method :
-(void)viewWillAppear:(BOOL)animated
{
}
What's the best way to release the Objects?I want to release all the used Object when my View closes.
Thanks

You should release them after your view has disappeared, not before, otherwise you may see some strange effects if your view disappears with animation...
- (void)viewDidDisappear:(BOOL)animated {
// release here...
[super viewDidDisappear:animated];
}

release them here
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
}

Related

call viewDidDisappear in Tabbar based Application

I am using tab bar based iPhone application.I have set NSTimer in first two tabs.i want to invalidate this timer. so i am invalidating in viewDidDisappear. But when i am clicking on different tab, it will never call viewDidDisappear. I don't know how to call this method?please help me...thanking you in advance...
are you put [super viewWillDisappear:animated]; in viewWillDisappear some time it happaned becouse we forget to put this line in to viewWillDisappear
- (void) viewWillDisappear:(BOOL) animated {
[super viewWillDisappear:animated];
}
set time invalid in viewWillDisappear: method , like bellow..
-(void)viewWillDisappear:(BOOL)animated
{
[yourTimer invalidate];
yourTimer = nil;
}

Back button on Navigation controller dont stop video streaming (IOS)

When i push Back button on Navigation controller app is successfully return to the controller view, but dont stop video streaming.
I write:
- (void)viewDidUnload
{
[self setDescText:nil];
[super viewDidUnload];
[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];
// Release any retained subviews of the main view.
}
but not help.
Please, give me advice.
viewDidUnload is only called in low-memory conditions. You want viewDidDisappear.
- (void)viewDidDisappear:(BOOL)animated
UIViewController Class Reference

How can i refresh a view every time when a tab bar item is clicked?

I am making an application with TabBar. But TabBarController is not RootViewController. In the Tab Bar there are 4 tabs. One of those tabs is history which is linked with a table view which shows history. I want to refresh that view every time when i click that so that i can get updated tableview. How can i do that? Thanks in advance.
use - (void) viewWillAppear:(BOOL)animated to update any content in your view.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// You code here to update the view.
}
This method will be called every time the view is about to be displayed.
below code added plz ^^
If you change a tabBarIndex, At the same time -(void)viewWillAppear called.
-(void)viewWillAppear:(BOOL)animated
{
// force the tableview to load
[tableView reloadData];
}
refer a Apple Sample Code: that is amazing great tutorial for you about UITabBarController
http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
Apple Sample Code is no added [super viewWillAppear:animated];
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
if ([Globals sharedInstance].isFromFindSimiler)
{
[self.view addSubview:_findSimilerView];
[_historyTableView setFrame:CGRectMake(0, 85, 320, 490)];
}
else
{
[self history_webservice];
}
}
I have achieved this. In this case, whenever user tabs on History screen I call web service for history and update tableview.
instead of using (void)ViewDidLoad use viewWillAppear:(BOOL)animated
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//your code
}

When I press back on my UInavigation bar while a webview is loading, my app crashes...?

I have a view with a navigation bar in my iphone app. The navigation bar has a back button on it to go the the previous view. Within the view is a webview. If the webview is loading while the user presses the back button on the navigation bar, the app crashes with the error message:
[WebViewController respondsToSelector:]: message sent to deallocated instance
I am assuming that I release some things when the view unloads. I believe my viewWillDisapear method takes care of stopping the webview from loading... But still, when I press back while the view is not fully loaded, it crashes.
It seems to happen when I let the view finish loading about 20% of the time - and only if I press the back button really quickly after the page finishes loading.
Here is the code that I believe runs during this shinanigans....:
- (void)viewDidLoad {
[super viewDidLoad];
webView.scalesPageToFit = YES;
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: url]]];
[addressBar setText:url];
}
and then when the webview is loading:
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (first) {
//mtns is a uiimageview
mtns.alpha = .8;
first = NO;
}
[activityIndicator startAnimating];
}
and after a suggestion, I added this method:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.webView stopLoading];
self.webView.delegate = nil;
}
I don't know why it is crashing so I am sort of at a loss
Thanks,
R
You could override -viewWillDisappear: to stop the UIWebView from loading and set the delegate to nil.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.webView stopLoading];
self.webView.delegate = nil;
}
This may be because of before the execution code that you have written in viewwillDisapper application crashes.try to put breakpoint and check whether viewwillDisapper method is called or not.If its not called then write the code that mark has suggested before the application navigates to second view controller.
You have to be sure to set the delegate property of the web view to nil before it goes away. Do this both in your dealloc and viewDidUnload methods, since viewDidUnload is not always called before dealloc. What’s happening is that after your view controller is deallocated, the web view still has a reference to its address, and is trying to send it a message (-respondsToSelector:).
- (void)dealloc {
webView.delegate = nil;
[super dealloc];
}
- (void)viewDidUnload {
webView.delegate = nil;
[super viewDidUnload];
}

Race condition in refreshing view when application becomes active

On Facebook's iPhone app, the news feed refreshes every time the app becomes active. I would like to do something similar, but I'm concerned about a race condition. The general bootstrapping of my app is as follows:
UIApplicationDelegate
- (void)applicationDidFinishLaunching:(UIApplication*)application
{
[window addSubview:[self.navigationController view];
[window makeKeyAndVisible];
}
- (void)applicationDidBecomeActive:(UIApplication*)application
{
[rootViewController refresh];
}
RootViewController
#pragma mark custom
- (void)refresh
{
if (self.newsFeedModel == nil) {
self.newsFeedModel = [[NewsFeedModel alloc] initWithDelegate:self];
}
[self.newsFeedModel request];
}
#pragma mark UIViewController
- (void)viewDidLoad
{
// initialize the table
// add subviews and whatnot
}
#pragma mark NewsFeedDelegate
- (void)newsFeedSucceeded:(NSMutableArray*)feed
{
// reload table view with new feed data
}
After sprinkling NSLog everywhere, I determined the order of operations to be:
applicationDidFinishLaunching
applicationDidBecomeActive
refresh
viewDidLoad
newsFeedSucceeded
Notice how refresh is called before the root view has been loaded. While we're busy querying the server, the root view loads. When the server responds, the root view is populated with the feed. This works in most cases because the network operation takes a long time. However, if the network operation finishes faster than view can be loaded, then I will be attempting to construct the news feed before the view has been loaded. This would be bad. What is the best Cocoa Touch practice for solving this race condition? I would just set a bunch of flags to determine what state we're in and refresh the news feed depending on the state, but I'm wondering if there were built in events in Cocoa Touch to handle this for me.
I think you want to take a look at applicationWillEnterForeground: instead.
applicationDidBecomeActive: can be called while your app is still running in the foreground. For instance if a text message comes while your app is in the foreground and the user dismisses it, applicationDidBecomeActive: will get called.
You can subscribe to the UIApplicationWillEnterForegroundNotification event in your RootViewController using NSNotificationCenter. I would do this in RootViewController initWithNibName: or whichever init method you are using.
Now you just need to call refresh in 2 places. Once at the end of viewDidLoad and again whenever applicationWillEnterForeground: is called.
This should solve your race condition problem. Since RootViewController is handling it's own refreshing when it knows it is ok to do so.
RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
- (void)viewDidLoad
{
// initialize the table
// add subviews and whatnot
[self refresh];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self refresh];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}