Asking iOS to reload the ui or refresh it - iphone

I display a view, and the data to populate one of the picker views comes down over the network after the app has loaded the view, so the picker view gets passed null so shows no items to pick from.
Is there away I can tell the UI to reload?
The view hierarchy is
Tabbar->Navbar->scrollview->Pickerviewcontroller
If the above is relevant.

assuming you have the datasource set up correctly, once it gets the data, call reloadAllComponents.
[pickerViewController reloadAllComponents];

You can use NSNotificationCenter to post a notification to update the picker when the data has finished loading
Create the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshPicker) name: #"WEBSERVICE_UPDATED" object:nil];
Post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"WEBSERVICE_UPDATED" object:lesson];
-(void)refreshPicker {
// Handle refresh here
[pickerViewController reloadAllComponents];
}

Related

Change UI immediately after receiving UIApplicationWillResignActiveNotification

I need to make changes in my UI immediately when I receive UIApplicationWillResignActiveNotification.
Here is what I do:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
- (void)applicationWillResignActive:(NSNotification *) notification {
// my changes (hide some views, change bg color, change text, etc)
// ...
}
The problem is that those changes are applied only after the application returns to the active state. So the user can see for a moment the previous state of UI.
Is there any workaround to solve this problem?

In terms of iphone application, the way to reload displayed page automatically when a background app comes back to be active

I have developed an iphone application with Phonegap/Cordova v1.9.0.
I want to realize the following matter.
-When a background app comes back to be active(When the app icon is tapped), a displayed page is reloaded automatically-
Probably I should make some programs in a function, (void)applicationDidBecomeActive, in Appdelegate.m or MainViewController.m, but I have no idea what to do.
Please tell me how to solve this case.
You can use the NSNotification observer pattern. In your MainViewController.m file, and viewDidLoad, you can add an observer (registering for notifications):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
Then you must implement appDidBecomeActive: (you can give the selector any name, but you must implement a method of that name). In this example:
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(#"App became active");
}
When the app is resumed, and should this view controller be active, it will simply log that to the console. You can put any code you wish inside that method (in your case, refreshing a page).
Don't forget to remove the observer when the view controller is deallocated in the dealloc method. This will remove all observers for you.
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSLog(#"Removed all notification observers");
}
Are you really using version 1.9?
Try this:
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event, reload the page or content
}

Performing an action on one view, and the action doing something on another view? Is this possible?

I have 2 views:
OneViewController
TwoViewController
TwoViewController has an IBAction which plays a sound. Once the user has pressed the button on TWoViewController I want a UILabel which will appear on OneViewController saying that the sound has been played.
Thanks for the help
All you have to do is reference one viewController in the other one, that way you can call it's methods. Or you can simply create a delegate.
One possible solution is to use notifications.
In the action that plays a sound, post a notification to the default notification center that indicates the sound has played.
[[NSNotificationCenter defaultCenter] postNotificationName:"playSoundNotification"
object:self
userInfo:nil];
When OneViewController is created, have it register for the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(showPlayedLabel:)
name:"playSoundNotification"
object:nil];
When it receives the notification -- in showPlayedLabel: -- display the UILabel. Note that showPlayedLabel must follow the appropriate signature format.
- (void) showPlayedLabel:(NSNotification*) aNotification;

Returning from dismiss modal view controller - any way to detect?

Wonder if anyone can help me. I have a setup whereby a main menu presents the main game as a modal view controller. At this point it also stops playing the main menu music.
The problem is that when the game view controller dismisses itself (e.g when user quits game) and returns to the main menu I cannot get the main menu music to start playing again.
Is there some way I can get the music in the main menu to start playing again ? E.g a delegate method that is called when the main game dismisses ?
Thanks,
Martin
You can do it with the NotificationManager
// set up notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMusic:)
name:#"musicNotification"
object:nil];
// send notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"musicNotification"
object:self];
// clean up notification
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
- (void) playMusic:(NSNotification *) notification
{
// play the music here
if ([[notification name] isEqualToString:#"musicNotification"])
NSLog (#"Received musicNotification!");
}

Iphone- How to resize view when call status bar is toggled?

I'm creating an iphone app with few elements inside the controller (e.g tab bar, uiview, uitoolbar, etc..). Everything works fine until I encountered this problem. While my application is launched, I received a call and it shows the "Call Status Bar" which ruined the ui. Some elements are pushed down because the "Call Status Bar" is taking space at the top.
Anybody here have an idea on how to fix this issue? I'm new to iPhone app development.
Your reply is greatly appreciated...
Best Regards,
dianz's solutio works just fine but is a bit redundant if you are only interested in knowing about the notification inside of a specific view controller.
After the delegate method application:didChangeStatusBarFrame: is called in the Application Delegate UIApplicationDidChangeStatusBarFrameNotification is posted through [NSNotificationCenter defaultCenter].
Instead of using the delegate method application:didChangeStatusBarFrame: to simply repost a custom notification you can add an observer to UIApplicationDidChangeStatusBarFrameNotification directly from your view controller.
In MyCustomViewController you would add something similar to this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doSomething:)
UIApplicationDidChangeStatusBarFrameNotification
object:nil];
Now you no longer need to define the application:didChangeStatusBarFrame: delegate method in appDelegate (unless you plan to do something in the appDelegate when the status bar changes size).
As with dianz's example you need to remove the observer in dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
You should put this function on appDelegate, this will trigger when the status bar change
- (void)application:(UIApplication *)application didChangeStatusBarFrame (CGRect)oldStatusBarFrame
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"trigger" forKey:#"frame"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"trigger" object:self userInfo:dict];
}
This Code will send Notification with the name "trigger"
Place a code to your view Controller (e.g: viewDidLoad, etc..) this listen if there are notification send with a name "trigger"
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dataReceived:)
name:#"trigger"
object:nil];
And create a function dataReceived:
- (void)dataReceivedNotification:(NSNotification *)notification {
NSDictionary *data = [notification userInfo];
// do something with data
}
do something on this part of the code, maybe you change the frame of your tab bar, uiview frame, toolbar frame
And in dealloc, put this code to remove the observer
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Basically what you normally do is try to set up the autoresize flags of all your ui elements in interface builder so that when the main view is "squashed" by the call status bar everything will still look reasonable. It's a little hard to explain how to do all of this in one message, but I recommend creating a view in IB, placing some subviews in it, then resizing the main view while playing with the autoresize flags to get a feel for how the flags work. The autoresize flags are in Command-3 (size inspector).
You can also set wantsFullScreenLayout in the main view controller to YES to cause the view to take up the whole screen, including the area under the status bar, but then you'll have to make sure not to place anything under the status bar and the call status bar will overlap anything too close to it, of course.
for me, whenever the status bar is enlarged, the -(void)viewWillLayoutSubviews is always called. This is perfect for me because all my subview setFrame code is in this function.