Refresh/reload view after syncing on the Internet - iphone

I'm writing iPhone application which periodically is syncing with the synchronization web service. Everything works ok, but unfortunately after synchronization user do not see any changes in the view if he visited it before.
I need to force to reload/refresh some of views. How could I do that with for example viewWillAppear method?
I've tried something like this in one of my view controllers:
-(void) viewWillAppear:(BOOL)animated
{
NSArray* sbw = [self.view subviews];
UIView* view;
for (view in sbw)
{
[view setNeedsDisplay];
}
[self.view setNeedsDisplay];
[super viewWillAppear];
}
But the subviews are not refreshing. Is there any method for forcing to reload/refresh view?
I know that viewWillAppear is executed when I move to this view but it's not refreshing - so how can I do that?

in tableviews you could use : [tableView reloadData]; to reload a tables content. If you just want to update the views content you could just change it. Lets say if you have a view class and a controller class MyUIView and MyUIViewController and MyUIView does contain an UILabel * myLabel you could change the label text inside the controller by:
[((MyUIView *)self.view).myLabel setText:#"This is a new text."];
so no need for forcing update.

You can write the method "viewWillAppear" in that you can write your code to be reloaded which may take the data from your data base.This method will be executed when ever the view starts to load.

Related

How to refresh UIViewController programmatically?

I have a ViewController in which the user selects a card (a custom UIButton) out of a UIScrollView. I have intercepted the touch event selecting the card and identified it, and then removed it from the data source, but it still exists in the UISubView. How do I get rid of it? Refreshing the view should show it removed from the view. How do I do that?
you can do it in one of two places:
in your viewcontroller
directly in the view
you need to call the function setNeedsDisplay
if you do it from the viewController then [yourViewOutletVariable/viewParameter setNeedsDisplay];
if you write it in the view itself then [self setNeedsDisplay];
hope this helps
You can either let view controller observe your models or update your views manually.
I'm not very clear about your question, what is still remaining on your view?
For automatically update views when model changes, I suggest ReactiveCocoa.
Once you have a handle on your view:
UIView *v = ...;
[v removeFromSuperview];
You could also call the setNeedsDisplay method on your scroll view after calling removeFromSuperview.
If your point is to refresh "UIViewController", then:
[self viewDidLoad];

Reload data for UIView\UIViewController?

When I am moving the buttons on the screen from a function, [self makeButtons], nothing happends unless I push a viewcontroller, then pop back. Is there a reload data function for ViewController, as it is for UITableViews? I am using NavigationController, and I am adding subviews to a UISrollView and moving other buttons on the screen. The method is called after fetching data with ASIFORMHTTPRequest.
EDIT: I am sorry I didn't specify more.
I have a method that is sending a [request startAsynchronous] (ASIFORMHTTPrequest).
I have a NSMutableArray containing all my buttons. When the request is done, a method called doneGettingRequest, which looks like this.
- (void) doneGettingRequest(ASIFORMHTTPRequest *) request {
[self removeButtons];
}
which is calling this method;
- (void) removeButtons {
NSLog(#"Removing buttons!");
for (UIButton *button in gameButtons) {
[button removeFromSuperview];
}
Everything works when I go to another view, then back again. The problem is it won't refresh if THAT view is being shown when the method is called (which will happend almost always). The gameButton is a NSMutableArray containing buttons that are currently being showed. When I am done removing them, I want to add some other buttons. The problem is, the buttons is not removed when the removeButtons is called. The message "Removing buttons!" however, is shown, so the method is being called.
Any suggestions?
Thanks
You can put your logic in your viewWillAppear.
This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view.
You can override this method to perform custom tasks associated with displaying the view.
If you override this method, you must call super at some point in your implementation.
Have you tried
[view setNeedsDisplay];

Update UITabBar Views?

I have an application where I was using buttons on a toolbar to call up the views, but I am switching it over to using a tab bar.
When using the buttons I was using the following code in MainViewController.m to update the values on the page and it was working just fine:
-(IBAction) loadSummaryView:(id) sender {
[self clearView];
[self.view insertSubview:summaryViewController.view atIndex:0];
//Update the values on the Summary view
[summaryViewController updateSummaryData];
[summaryViewController calculateData];
}
However, with the Tab Bar I can not figure out how to do this. I tried putting all of the necessary code in the Summary Views viewDidLoad and it loads the initial values, but it will not update the values when I change them in another view.
Any help is appreciated. I am a bit new at this, so please don't be cryptic as I may not understand the response.
Thank you.
By placing your code in viewDidLoad, it will only be called when the view is loaded from the nib. Unless you're running low on memory, this view will remain loaded for the duration of the life of your app.
So if you need to update values every time the view will appear, consider moving that code to an override of viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// your stuff goes here...
[self updateSummaryData];
[self calculateData];
}
You can update your current view in the viewWillAppear:animated message of the view:
If you have everything setup correctly, there is nothing to do, the UITabBarController will show your view, an your UIViewController will receive a viewWillAppear message, where you can do your update:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self updateSummaryData];
[self calculateData];
}
I strongly recommend reading the View Controller Programming Guide for iOS which describes in detail the main interface paradigms supported by the iPhone.

How to set a subview's delegate to the superviews controller

In IB I created UIView inside of a UIScrollView. File's owner for both is a UIViewController named JLViewController. The UIView is wired to class BodyClock.
The BodyClock class draws a graph inside the view. It also creates several small views which act as touch hot spots. When a hot spot is touched it displays an informative alert with a button for more detail. I need to tell JLViewController to display the detailed information. I thought I could do this by making the ViewController the HotSpot's delegate. Being as how I am creating the hot spots in the BodyClock class, I can't figure out how to set the hot spot delegate to JLViewController. I am trying to do something like this..
//Code in BodyClock
//create the hot spot
id viewController = [self nextResponder];
HelpHotSpot *helpHotSpot = [[HelpHotSpot alloc] initWithFrame:CGRectMake(start_x, melatoninHeightEnd_y, 80, 40)];
helpHotSpot.delegate = viewController;
[viewController addSubview:helpHotSpot];
[helpHotSpot release];
//Code in the HotSpot after touch and request for more info
//notify JLViewController to display the details
if ([self.delegate respondsToSelector:#selector(hotSpotMore:)]) {
[self.delegate hotSpotMore:itemDetails];
}
Everything works except that respondsToSelector fails. If I NSLog viewController or self.delegate I get...
UIScrollView: 0x7443c20; frame etc...
I was expecting JLViewController: instead of UIScrollView: so I think this is the problem.
How do I set the delegate to the ViewController for these subviews?
Is it possible, or should I be using notification instead?
This:
id viewController = [self nextResponder];
isn't doing what you think it is doing. This doesn't get a view's controller but instead looks at the chain of responders and determines which is the next one for responding to events - these are typically views or controls (not to be confused with controllers). It appears that after your hot spot view, the scoll view is next.
You'll have to get the pointer to the controller correct. Maybe in your hot spot view you could add:
IBOutlet JLViewController *viewController;
And in interface builder connect this to your view controller.

Multiple view controllers with their own xibs, and when they load

Going through many tutorials and with the help of everyone here, I am trying to wrap my head around using multi view controllers with their own xib files.
I have one example where there is a : multiViewController and two others: aboutViewController, rulesViewController.
In both aboutViewController.m and rulesViewController.m files, I have placed the following code:
- (void)viewDidLoad {
NSLog(#"rules View did load"); // (Or About View did load, depending on the .m file)
[super viewDidLoad];
}
The mainViewController.m file contains:
-(IBAction) loadRules:(id) sender {
[self clearView];
[self.view insertSubview:rulesViewController.view atIndex:0];
}
-(IBAction) loadAbout:(id) sender {
[self clearView];
[self.view insertSubview:aboutViewController.view atIndex:0];
}
My question is, why is it when I run the application does the ViewDidLoad for both About and Rules fire? Meaning, I get the NSLog messages. Does this mean that regardless of the separate xib files, all views are loaded on start up?
My point of all this is: I want the multiViewController to be my main menu which will have separate buttons for displaying the other xib files when clicked. I had been placing my "initalize" code in the viewDidLoad but this seems wrong now as I don't want to hit until the users presses the button to display that view.
An example was to have a view that is: playGameViewController. In the viewDidLoad I was checking to see if a prior game was in progress and if so, prompt the user if they would like to resume. In the above case, when the app starts, it prompts right away (because the viewDidLoad is firing) even though I only wanted to display the main menu first.
Any explanation is greatly appreciated. Thanks in advance.
Geo...
My question is, why is it when I run
the application does the ViewDidLoad
for both About and Rules fire?
Meaning, I get the NSLog messages.
Does this mean that regardless of the
separate xib files, all views are
loaded on start up?
When you call
[self.view insertSubview:rulesViewController.view atIndex:0];
it's going to first call viewDidLoad for the initial view and then viewDidLoad once again for RulesViewController.
When your MainViewController, or any view for that matter loads, viewDidLoad is called automatically. ViewDidLoad is there in order for you to override or modify any objects in the nib, or you can create objects yourself. Views are only loaded on an as needed basis. If you were to load all your views initially when the app boots up, the user would just see a black screen until all the views are processed.
You say you are going through some tutorials, I don't know your area of expertise yet, but have you looked into navigation based apps using UINavigationController?
Just an example as your request if you want to have a button load a view you can do something like.
- (IBAction)pushSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
if (secondView != nil) {
secondView.title = #"Second View";
[self.navigationController pushViewController: secondView animated: YES];
}
}