Refresh view in iPhone navigation based application - iphone

I am using a navigation based application for iPhone. I want to reload a view when i come back to it after pressing the back button. ANy solution?
Regards

Add a method viewWillAppear: to your controller class. In that method you can then update the view with current data.
The viewWillAppear: method will execute whenever the view is about to be displayed (after navigating to a different view using UINavigationController)

There is more than one, but I usually use NSNotificationCenters. You attach "listeners" for some kind of event, like this:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onSomethingChanged:)
name: #"somethingChangedEvent"
object: nil];
So, if some other view changes a setting, it notifies all the listeners like this:
[[NSNotificationCenter defaultCenter] postNotificationName: #"somethingChangedEvent" object: Nil];
Pretty simple and intuitive.

Related

What gets called after UIApplicationDidBecomeActiveNotification gets triggered?

In my app I'm trying to make my navigation bar not shrink from 44px to 32px when the phone is rotated to horizontal orientation. I've been able to accomplish this by setting the navigationBars frame when the view is rotated and also in viewDidAppear. However, when I push the home button to exit the app and then I reenter the app, the navigation bar still shrinks. So I implemented a notification to detect UIApplicationDidBecomeActiveNotification, and in that method I reset the navigationBar frame height to 44px. However, it doesn't work because something is getting called which is resetting my views frame. Does anyone know what gets called after a UIApplicationDidBecomeActiveNotification gets triggered that resets the viewcontrollers frame?
In Your application any class can be an "observer" for different notifications. When you create view controller, you can register it as an observer for the UIApplicationDidBecomeActiveNotification and specify which method that you want call when that notification gets sent your your application.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name: UIApplicationDidBecomeActiveNotification object:nil];
remove observer in ViewWillDisappear/viewDidDisAppear/Dealloc as per your need:
[[NSNotificationCenter defaultCenter] removeObserver:self];
I'm having the same problem, if you attach an observer via Key Value Observers you can see that something is called after the UIApplicationDidBecomeActiveNotification.

Kill a view/View controller from memory from another View Controller

I am fairly new to Obj-C programming, so please bear with me.
I have a Tab Bar. 1 of the tabs has a table view controller that gets it's data from an SQL database into an array.
In another tab it has a button to reset everything (delete all data). The view controller for this tab successfully deletes the SQL database, however the table is still full of data when going back to the table tab.
How can I unload the table view controller from memory when the button is pressed from this other view controller?
If I simulate a memory warning, then the table clears and is rebuilt when going back to the tab. This is what I am trying to do in code.
Thanks
You need to call reloadData on that UITableView.
It is cleared during memory warning, cause a view controller recreates its own view every time it receives this warning (in case it is not currently visible).
I think you could accomplish this using NSNotifications.
In the init method of your 1st view controller put this code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(tableViewDataUpdated:)
name:#"DATA_UPDATED"
object:nil];
Create a tableViewDataUpdated method that reloads the array containing the data, and then calls reload on the table view.
In the 2nd view controller, when sql data base gets cleared, call
NSNotification *notification = [NSNotification notificationWithName:#"DATA_UPDATED" object:nil userInfo:nil];
[[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
Finally in the dealloc method of the 1st view controller include:
[[NSNotificationCenter defaultCenter] removeObserver:self];

How do I access a UIViewController's button from a UIView?

I know there are similar questions out there, but please consider mine before writing it off as a duplicate.
I have a true/false that the View Controller receives from user input and uses to adjust the selected state of a button, then passes the true/false data to a UIView where a method is run. Once the method is done I would like to return the selected state of the button back to NO, however when I try to tell the UIView to access the ViewControllers button I run into some problems.
Could someone please tell me how I might do this and, if it's not possible, could you outline where I have strayed from the MVC pattern and how I might get back on track?
I would use an NSNotification. Easier to get your head around than delegation if this is all new to you.
register something like this in your parent view controller:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(methodToChangeButtonState) name:#"changeButtonState" object:nil];
and call it at the appropriate time from your child viewcontroller:
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeButtonState" object:nil];
Your methodToChangeButtonState will fire when the notification is posted updating your button state.

update favourite list in realtime iphone

My app has two tabs, one of which is a table view, in the other tab I can add the current object to the core data storage, and I wish the table view could be up-to-date anytime I am done adding and switch back to that tab (table view). Currently, the table view only refreshes when my app relaunches which is understandable, because I am getting the data in the viewDidLoad method in that viewController. When I switch back and forth between these two tabs, their views are already loaded, so how can I update the table view in realtime? Any advice would be appreciated.
Update:
A good example is the contact app on iphone, but I don't know how to do that...
Here is some code in the table view controller.
- (void)viewDidLoad {
[super viewDidLoad];
bList = [[NSMutableArray alloc] init];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//some code to get data from core data storage and put them in the bList array.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveAddNotification:)
name:#"AddNotification"
object:nil];
}
-(void) receiveAddNotification: (NSNotification *)notification{
if ([[notification name] isEqualToString:#"AddNotification"]){
NSLog (#"Successfully received the add notification!");
}
}
Code in the other tab's view controller.
-(IBAction) addSomething {
[[NSNotificationCenter defaultCenter] postNotificationName:#"AddNotification" object:self];
//some code to store an object in core data.
}
The console can output the message "Successfully received the add notification!", which means the notification works fine, but the table view didn't get refreshed when I switch back to the table view tab from edit tab. I'm assuming the newly added object in addSomething method should be updated to the receiveAddNotification: method either.
Hi Michael as I understand your question, You can use notifications concept. Start notification service in the table view controller and when you are done with adding the things you can post the notification so that, that will update the table view automatically.
Look in to the NSNotificationCenter.
Let me explain with an example.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTestNotification:)
name:#"TestNotification"
object:nil];
add the above statement in the table view controller. Then you just define the "receiveTestNotification:" function with [tableview reload];
And in the editing view controller when user taps the done button you post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"TestNotification" object:self];
So it will call the "receiveTestNotification:" method and run the code what ever you gave in that method.

App Delegate - Unload View Controller

I'm trying to unload a view controller from view when the iPhone goes to sleep. My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out.
I have all this functionality in place, I'm capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. It all works beautifully. When I was doing some additional testing I realised I hadn't catered for the iPhone going into sleep mode.
So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. I know the following method is called when the app goes to sleep:
-(void)applicationWillResignActive:(UIApplication *)application
How do I unload the stopwatch view controller from my app delegate ?
---- UPDATE ----
kpower, thanks for your feedback. I've implemented the following code:
In my App Delegate:
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"AppIsAsleep" object:nil];
}
In my view controller, I have the following:
-(void)viewDidLoad
{
// Add Observer.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewDidUnload:) name:#"AppIsAsleep" object:nil];
}
- (void)viewDidUnload {
//Remove the Observer.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"AppIsAsleep" object:nil];
}
When the phone goes to sleep, it actually closes the app, am I doing something wrong ?
Regards,
Stephen
You can use the Notifications mechanism. It allows you to unload view controller from different place (not the AppDelegate) this case.
For example, in your view controller's viewDidLoad method you add an observer (don't forget to remove it in viewDidUnload) and in applicationWillResignActive: method of AppDelegate you just simply post notification. That's all.
↓ Update here ↓
When you get a notification - you should manage view controller's removing by yourself. And calling viewDidUnload here is not the solution, cause this method is called after view controller was already unloaded and doesn't cause removing.
How to remove? Depends on how the view controller was added (for example, popViewControllerAnimated for UINavigationController). The main idea here is to make object's retain count equal to 0 (as you know this case an object will be destroyed) - so you should sent release message necessary amount of times.