UINavigationController suggestion - iphone

I have read that UINavigationController are best option when you want to jump from n number of screen to first screen. It takes following code to do so:
NSMutableArray *array=[[NSMutableArray alloc]initWithArray:self.navigationController.viewController];
[array removeObjectAtIndex:1];
[array removeObjectAtIndex:1];
[array removeObjectAtIndex:1];
self.navigationController.ViewController=array;
[self.navigationController popViewController:YES];
by using this code I can go directly from fourth screen to first screen directly .
If I don't using navigation controller then also by making the object of firstSCreen in fourth screen I can achieve the same thing within couple of lines.Then why one should go for navigation controller? If answer is for memory optimization then we are autoreleasing the the object of firstViewController and now we are using auto referencing.

first thing this is wrong approach to pop. true one is
[self.navigationController popToRootViewController:YES];
and second thing is that if you are at 4 screen, then by poping to a specific viewcontroller will no pop all screens. here is memory issue.
another issue is that some times you don't want to lose parent screens data. if you use method of pop to specific viewcontroller, the data will be lost as the object having data is released. you made a new one.
point is that this depends on your conditions what is suit able in your scenario. but the normal and usual approach is that don't use specific popout techniqu as it may cause problems

You can use
[self.navigationController popToViewController:(UIViewController *) animated:(BOOL)];
You just need to pass ViewController's object on which you want to move directly... And between view controller's will pop-out automatically
Hope this helps you...

agreed with 'The Saad'
one more thing is that if you have various screens with the data coming from server.
it will be quite difficult to render(user interection) screen second while loding data on first.
same with all other sibling views.

Related

Refreshing the content of TabView

Ok I am trying to refresh the tab content of each of my tabs after a web call has been made, and I have tried soo many different methods to do this that I have lost count. Could someone please tell me how this is possible?
The web call just calls JSON from a server and uses it to update the content of the tabs. For testing purposes I have a button set up inside my settings class. Settings class is a view within the home tab which has a button called refresh. When clicked this takes JSON stored on the device which is different to the one called from the web call on application start up. This saves me having to change the JSON on the server.
I will take you through some of the techniques I have tried and would be grateful if someone could tell me what I am doing wrong.
I tried making an instance of the class and calling the refresh method like this
DashboardVC *db = [[DashboardVC alloc] init];
[db refreshMe];
The refresh method in dashboard class is this
-(void) refreshMe
{
[self loadView];
[self viewDidLoad];
}
However no luck. This method will work if I call it inside the Dashboard class, but wont work if I call it from another class. I think it is become I am instantiating a new class and calling refresh on that. So I dropped that technique and moved onto the next method
This loops through all the tabBars and changes the tabTitles without any issues, so it I know it is definitely looping through the ViewControllers properly.
I also tried every varient of the view methods like ViewDidAppear, viewWillAppear etc, no luck.
I also tried accessing the refreshMe method I made in the dashBoard class through the tabController like this
[[[self.tabBarController viewControllers] objectAtIndex:0] refreshMe];
But again no luck, this just causes my application to crash.
I read through this guide
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerPGforiOSLegacy/TabBarControllers/TabBarControllers.html
on the apple website but it doesn't seem to cover how to refresh individual tab content.
All I want is to have each individual tab refresh its content after the web call is made, and have spent ages trying to figure this out, but nothing is working.
So would be very grateful if someone could show me what I am doing wrong?
Thanx in advance....
EDIT:
Expand on what I have tried
After discussion with Michael I realised you should never call loadView as against Apple guidelines. So I removed any references to LoadView. I have now placed a method in all the main ViewControllers called RefreshMe which sets up the views, images texts etc in the class. And this method is placed inside the ViewDidLoad. Now I want to be able to call these methods after a web call has taken place, so effectively refreshing the application.
My viewDidLoad now looks like this in all my the main classes.
- (void) viewDidLoad {
[super viewDidLoad];
[self refreshMe];
}
And then the refreshMe method contains the code which sets up the screen.
The JSON data pulled from the web call will set up the content of each of the 5 tabs, so need them all to be refreshed after web call.
I tried looping through the viewControllers and calling viewDidLoad, which should in turn call the refreshMe method which sets up the class, however nothing happens. Code I used was this
NSArray * tabBarViewControllers = [self.tabBarController viewControllers];
for(UIViewController * viewController in tabBarViewControllers)
{
[viewController viewDidLoad];
}
For the time being I have also included
NSLog(#"Method called");
in the viewDidLoad of each class to test if it is being called. However the message is only being printed out when I first load the application or if I re-enter the application. This method should be called after I click the refresh button in the settings screen but it isn't and I have no idea why.
Anyone have any idea why this is not working?
From the question and your comments, it sounds like there are at least two problems:
You're having trouble accessing the view controllers managed by your app's tab bar controller.
You seem to be working against the normal operation of your view controllers.
The first part should be straightforward to sort out. If you have a pointer to an object, you can send messages to that object. If the corresponding method doesn't execute, then either the pointer doesn't point where you think it does or the object doesn't have the method that you think it does. Let's look at your code:
NSArray * tabBarViewControllers = [self.tabBarController viewControllers];
for(UIViewController * viewController in tabBarViewControllers)
{
[viewController viewDidLoad];
}
This code is supposed to call -viewDidLoad on each of the view controllers managed by some tab bar controller. Leaving aside the wisdom of doing that for a moment, we can say that this code should work as expected if self.tabBarController points to the object that you think it does. You don't say where this code exists in your app -- is it part of your app delegate, part of one of the view controllers managed by the tab bar controller in question, or somewhere else? Use the debugger to step through the code. After the first line, does tabBarViewControllers contain an array of view controllers? Is the number of view controllers correct, and are they of the expected types? If the -viewDidLoad methods for your view controllers aren't being called, it's a good bet that the answer is "no," so figure out why self.tabBarController isn't what you think.
Now, it's definitely worth pointing out (as Michael did) that you shouldn't be calling -viewDidLoad in the first place. The view controller will send that method to itself after it has created its view (either loaded it from a .xib/storyboard file or created it programmatically). If you call -viewDidLoad yourself, it'll either run before the view has been created or it'll run a second time, and neither of those is helpful.
Also, it doesn't make much sense to try to "refresh" each view controller's view preemptively. If your app is retrieving some data from a web service (or anywhere else), it should use the resulting data to update its model, i.e. the data objects that the app manages. When a view controller is selected, the tab bar controller will present its view and the view controller's -viewWillAppear method will be called just before the view is displayed. Use that method to grab the data you need from the model and update the view. Doing it this way, you know that:
the view controller's view will have already been created
the data displayed in the view will be up to date, even if one of the other view controllers modified the data
you'll never spend time updating views that the user may never look at
Similarly, if the user can make any changes to the displayed data, you should ensure that you update the model either when the changes are made or else in your view controller's -viewWillDisappear method so that the next view controller will have correct data to work with.
Instead of refreshing your view controllers when updating your tab bar ordering, why not simply refresh your views right before they will appear by implementing your subclassed UIViewController's viewWillAppear: method?
What this means is that each time your view is about to appear, you can update the view for new & updated content.

Reloading a table's data

I have 5 view controllers, which are in a navigation controller hierarchy. When I reach my last view controller, there's a button that lets me go back to the "Home" view controller (named CardWalletViewController) which is a table view controller. Here's the method I have in my last VC (PointsResultsVC)
- (IBAction)homePressed:(id)sender {
NSArray *VCs = [self.navigationController viewControllers];
[self.navigationController popToViewController:[VCs objectAtIndex:0] animated:YES];
}
CardWalletVC's cells is being filled up by values coming from the saved instances of a Card in NSUserDefaults and it works fine.
Now, what I want is to update the value in my CardWalletViewController, which is the points of a card coming from my PointsResultsVC. Note that this points is saved in NSUserDefaults.
Upon the process of trying to update of the value shown in my CardWalletVC, I placed [self.tableView reloadData]; inside -viewDidLoad, -viewWillAppear, and -viewDidAppear of the said class. I tried placing it one by one in each of this methods, yet it doesn't seem to work.
Advice Please.
EDIT: problem solved
This one served as my guide Save data from one tab and reloadData in another tab.
And as I have discovered, -viewDidLoad of a certain class will only be called once, all throughout runtime of app. Whereas -viewWillAppear, it will be called every time the view appears.
So, I just moved the way I am loading the values saved in NSUserDefaults from -viewDidLoad to -viewWillAppear. Also, inside -viewWillAppear I placed the [self.tableView reloadData]. Then there, problem solved.
There's a lot of things that could be going wrong so you'll need to provide more info to narrow it down.
First of all, viewDidLoad will not be called so you can remove it from here. Both viewWillAppear and viewDidAppear will be called, so you only need it one of these 2 methods.
Next, you need to determine what the actual issue is.
1 - is self.tableView a proper reference to the table? Are you properly maintaining the reference?
2 - is the table actually reloading but using the old data, or is it not reloading at all? You can log the willDisplayCell method to see what is going on when the view appears.
With that info, the root cause can be narrowed down to a solvable problem.

iPhone How To Save View States

I have been developing iphone applications for around 3months now and theres a few things that stump me and i don't really have an idea how to work round them.
I have a navigation controller controlling the views in my application however every screen that is loaded, used then pushed back loses all the information as it seems to be reinstantiated... I believe this is a possible memory management issue?
But how to i create an app that navigates and retains all information in its views until the application is closed.
Thanks :)
Possible you didn't keep a reference to the view controller, the issue is for UIVIewController not to be released.
Make the view controller an ivar you will instanciate only one time when you push it on stack.
// in .h
MyViewController *mVC;
// in .m
// maybe when the user selects a row in a tableview
if(mVC == nil) {
// first time use, alloc/init
mVC = [[MyViewController ....];
}
// then push on the stack
[self.navigationController ....];
Of course don't forget to release it later.
In this part:
MyViewController *myViewController=[MyViewController alloc] initWithNibName:#"myView" bundle:nil];
[[self navigationController] pushViewController:myViewController animated:YES];
[myViewController release];
You will probably have something like this... Instead, make your myViewController a class's property so you have a reference to it. And drop the [myViewController release]; statement.
Possibly your app is receiving a didReceiveMemoryWarning.
In such cases, when the super class is called, the framework does memory cleaning by unloading all the views that are not currently displayed. This could explain the behavior you are seeing.
To check it further, override didReceiveMemoryWarning in one of your view controllers or applicationDidReceiveMemoryWarning in your app delegate, and put a breakpoint in it. Don't forget to call [super...] appropriately, otherwise pretty soon your app will be killed. What you should see in this way is that the views do not disappear before hitting the breakpoint, and do disappear after that.
If the hypothesis is correct, you should find a way to save the state of your view in viewDidUnload and restore it in viewDidLoad. Look also at didReceiveMemoryWarning reference.
Try to save data in NSUserDefaults it its small or use plist or it its too small like 5-10 objects save in in some variable in appDelegate, and if its too large use sqlite and for saving something like images of files like xml use Document directory
The UINavigationController works like a stack: you push and pop UIViewControllers on it. That means when a UIViewController get popped, it will have its retain count decremented by 1, and if no other object holds a reference to it, it will be deallocated. You can avoid the UIViewControllers getting dealloced by keeping a reference to them yourself by calling -retain on the objects, for instance in your appDelegate.
You can use NSUserDefaults to save the states of the UIControls in the view.
So whenever u r loading a view, set the values to the controls so that it looks like it resume from the place where we left.

How do I pass a variable to a childController of UINavigationController based on the selected UITableViewCell

Imagine a simple Navigation based iPhone app.
The top level is a tableView with cells that read "PDF1" "PDF2" "PDF3" with disclosure indicators. Let's call the top level controller "RootController."
When you push the cell labelled "PDF1" a child controller class called "PDFViewerController" is pushed onto the stack which builds a new screen and loads "PDF1" in it's UIWebView.
Now imagine we go back to the tableView and push the tableCell "PDF2." This time the same "PDFViewerController" is pushed onto the stack but now it knows to load "PDF2".
The way I'm doing it now I have to write controller classes for PDF1, PDF2 and PDF3 creatively named "PDFViewerController1","PDFViewerController2","PDFViewerController3".
The thing is the only difference in these classes is the NSURL and it would be much less redundant code to be able to pass in the right NSURL based on a tableCell selection.
I feel like I'm missing something fundamental about OOP here. It'd be great if someone could point me in the right direction. I'm not even sure what kind of string to google to solve this. If someone can suggest a better title for this thread that might help others too.
Thanks.
Create an init method for PDF view controller with an extra parameter, then, in the didSelectRowAtIndexPath: method:
PDFViewController *pdfvc = [[PDFViewController alloc] initWithNibName:#"PDFViewController" bundle:nil andURL:(NSURL *)[myURLS objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:pdfvc animated:YES];
[pdfvc release];

Navigation Controller -- Server Driven Data

I have to show a navigation view where number of views are server dependent. So I cannot hardcode the view controllers. Is there any way I can use 1 view controller and the data will be sent by server. So essentially it will work like this:
1. Get data from server... show on nav view using nav controller.
2. Once I tap on one entity... I get another set of data from server... using the same controller show that on the screen... ans so on... as I am not sure till what level we can drill down.
3. Once user tap on the back button... I will use the data cached locally to present in the same view...
Do see any issue here. I am wondering if I can push the same controller class' object multiple times in the stack.
Please guide.
As long as they're different objects it will work fine.
YourNavViewController *firstNavViewController = [[YourNavViewController alloc] initWithNibName:#"YourViewXib" bundle:nil];
[self.navigationController pushViewController:firstNavViewController animated:YES];
[firstNavViewController release];
then later onto that one:
YourNavViewController *secondNavViewController = [[YourNavViewController alloc] initWithNibName:#"YourViewXib" bundle:nil];
[self.navigationController pushViewController:secondNavViewController animated:YES];
[secondNavViewController release];
etc.
No problem at all. You can dynamically push UIViewControllers into your UINavigationControllers viewController array. You should, as you mentioned, implement a mechanism for caching content on the device, so you don't have to reload everything all the time.
One approach would be through an xml structure that you load from the server only if it differs from what you have stored locally on the device (compare e.g. through hashes, version numbers of update timestamps).
You can't push the same object on the stack multiple times, but it sounds like you want to instantiate the same class multiple times, and push each of those objects.
So if the data you were collecting was XML (for example) and you had a hierarchy of objects you were parsing from an NSXMLParser class, for each level in the hierarchy, you could create a new view object containing the data at that level, and push that.