How to clear or resetting UINavigation history? - objective-c

I have a game setup screen which uses UINavigation and am trying to reset or clear the UINavigation once a person has selected a color.
My app's current process can be best described in the following diagram:
Start application -> New game -> Pick character -> Pick Color -> Start game
Up until "Pick Color" I use the UINavigation, however when a color is picked I want to clear the UINavigation history.
The reason for doing this is so that you can't go back once you've started the game and want the UINavigation to with a clean slate with no indication that you can go back (and this also includes going back to the main menu screen).
The way I am doing it right now is thusly;
[self.navigationController popToRootViewControllerAnimated:YES];
GameDashboardVC *dashboard = [[GameDashboardVC alloc] initWithNibName:#"GameDashboardVC" bundle:nil];
dashboard.title = #"Dashboard";
dashboard.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:dashboard animated:YES];
[dashboard release];
The problem is that it pops to the rootViewController but it never pushes the dashboard onto the stack.
I've tried:
[self.parentViewController.navigationController pushViewController....]
The only thing I haven't tried is putting my dashboard push inside the root view controller itself, but I am concerned because I am not sure if it should even be there.
Therefore, where is the correct place to put this kind of functionality, and how do I clear the UINavigation's stack.
Thank you for your time/help.

I solved the problem now to my satisfaction.
I put the following code inside my initWithNibName method:
self.navigationItem.hidesBackButton = YES;
This hid the back button and I didn't have to pop anything, but I think for memory reasons it might be useful to keep the idea of removing un-necessary items from the stack.
But for now the above will do.

If you want reset the UINavigation History, reinitialise the navigation controller.
if you using UINavigationController from AppDelegate class. You can reinitialise the navigation history.
appDelegate.navigationController=[[UINavigationController alloc]init];
Above code will reset the navigationController history..

Related

Objective-C Manage Several View Controllers

I'm new to Objective-C, and I'm looking for some advice on how to manage multiple view controllers.
I've looked through Apple's documentation on their built-in container view controller classes, and none of them seem to be what I'm looking for -- the closest is NavigationController, but even that seems a little bit off.
I want to implement a series of ViewControllers -- which use xibs for their interfaces -- that transition from one to the next according to a series of rules. For example, on app load, we see if we have a userId in local storage -- if we don't, show the signup screen. Next, there's a button to (say) order a taxi -- if that button is clicked, show the confirm screen.
Optional Aside: The reason I don't think this fits the Navigation controller is that the flow doesn't seem hierarchical, but rather kind of branchy and linear. One concrete example of this is that I don't need a navigation bar to go back, which seems to come standard on the Navigation Controller. But I don't know the NavigationController well enough to know for sure whether or not it fits this usecase.
I've been hacking this with a variety of methods. For example, in an IBAction handler, I've been using this code to transition to a new view controller:
UIViewController *view = [[UIViewController alloc] initWithNibName:#"CCWConfirmViewController" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];
Also, in my window's 'Root' ViewController (which I set to be the SignupViewController) initWithNibName, I return a different view controller than the one asked for, depending on the result of the local storage call I mentioned earlier:
if (currentUser.userId) {
// Instead of returning the SignupViewController, like was asked,
// return the MainViewController, since signup isn't needed for existing
// users.
CCWMainViewController *mvc = [[CCWMainViewController alloc] init];
return mvc;
I seem like I have to be doing something wrong (the second hack builds but generates a warning, since I'm returning a pointer to the wrong type). Anyone know a better way? Is the NavigationController for me after all, and I'm just misinterpreting its purpose? Do I just need to implement a custom container to serve as my RootViewController and manage these other ViewControllers?
Your decision is right. You'll not need a navigation controller for your purpose, but as they say.. There are a lot of ways by which you can achieve a result.
"I don't need a navigation bar to go back, which seems to come standard on the Navigation Controller"
You can always hide the navigation bar using self.navigationController.navigationBarHidden = YES
Coming back to the point, I would not say what you have done is wrong but would propose a better approach which involves the concept of view containment.
In cocoa touch you can add any view controller as a child view controller. So here's what I propose.
Create a class called RootViewController which will always be created and set to your window regardless of the condition the user is logged in or not. In the viewDidLoad of this class
-(void)viewDidLoad
{
if (currentUser.userId) {
CCWMainViewController *mvc = [[CCWMainViewController alloc] init];
[self addChildViewController:mvc];
mvc.view.frame = self.view.bounds;
[self.view addSubview:mvc.view];
}
else{
//Create signup/login view and add to view as above.
}
}

Getting viewDidLoad to be called when using popViewControllerAnimated

I'm using a UINavigationController to navigate between classes. When I press a button on the first view to send me to the second one, everythin works fine. But when I wan't to return from the second, to the first view, the viewDidLoad method isn't being called, and I really need that to happen.
According to this post, I should somehow set my view to nil but I'm not sure where or how to do that. This is the code that I'm using to return to the first view:
NewSongController *nsContr = [[NewSongController alloc] initWithNibName:#"mNSController" bundle:nil];
[self.navigationController popViewControllerAnimated:YES];
[nsContr release];
Your code is not correct.
You don't need to instantiate your first controller in order to pop to it. It already exists.
viewDidLoad will only run when you load the viewController for the first time (i.e. when you push to it). When you push to other controllers they are put onto a stack (imagine a stack of cards). When you push another card onto the stack the cards beneath it are already there.
When you pop it is like removing a card from the stack. But the card underneath is already there so it doesn't need to load again. All it does is run viewWillAppear.
All you need to do to pop back is...
[self.navigationController popViewControllerAnimated:YES];
That's it.
Remove the stuff about NewSongController (if that is what you are trying to go back to).
Then in the NewSongController function - (void)viewWillAppear:animated; put the code you want to run when you get back to it.
Hope that helps.
Your first view is loaded and is pushed onto the navigation stack. Dont try to mess with whats on the stack without fully understanding how setting the view to nil will affect the behavior.
Whatever you do on viewDidLoad, doing it in viewWillAppear or viewDidAppear will give you the result you want.
The viewDidLoad wouldn't appear because it already exists in the navigation stack. Since you are going back in the stack you would want to have the code that you want to trigger in viewWillAppear or viewDidAppear which is executed when popping from one viewcontroller to the one below it.

UIViewControllers problems

Hi there and thank you in advice for your help. I have a really strange problem while working with ViewControllers in Xcode4. First of all I have to say that I'm not using storyboards and I prefer to create any UI element programmatically. So I've set a UIButton and I want that, when pressed, it brings me to a new view controller. This is the code I'm using for a button:
-(void)settingsAndExportHandle:(UIButton *)buttonSender {
SettingsViewController* settingView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
settingView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:settingView animated:YES];
}
This buttons is initialized and allocated in the viewDidLoad method of the RootViewController. I want to switch to the other view controller (in this case SettingsViewController) when I press the button.
The strange thing is that when I press the button, the animation that flips the controllers goes well, but when it finishes I obtain the EXACT same things that I had on the RootViewControllers (same custom views, same buttons, same all!). The question is: what I'm missing?? I have to say that I use ARC (automatic reference counting) so I can't release or dealloc the views and buttons I've created on my RootViewController.
Any help will be appreciated. Thank you all!
Pushing and and modally presenting view controllers does not deallocate the view controller that presented them. It simply adds the additional view controller to the stack. You'll need to implement a callback method so that when the user hits the button to flip back to root view controller, your settings view controller lets the root view controller know what's about to happen so you can call a method you've written to reset the interface back to whatever state you need it at. You may also be able to use viewWillAppear: but that's a little messy.
However, according to the Apple Human Interface Guidelines, the user expects that when they push a view controller or modally present it, the view controller they were on will save state and be exactly the way they left it when they came back. It's disconcerting and annoying when state is not preserved while in a navigation controller context. It is especially annoying when it's modally presented.
Think about this - A user is in a hypothetical Mail app. They start typing out an email and set a font size and a color. They tap on the add attachment button, which brings up a modal view controller that allows them to select a picture. They select the picture and the modal view is dismissed, and in your implementation, the mail composing interface would have reset and the email content would be gone or at the very least the selected font size and color would be back to the default. That's not a good experience.

what am I doing wrong in attempt to popup a web view and then allow user to go back

my app has tabBarController with 3 views and in one of them I want to popup a web browser with the ability to return back to the application. To do that I am using UINavigationController.
In myAppDelegate.h I have defined the property UINavigationController *nav and in myAppDelegate.m I have #synthesize nav.
In the class where the webPopup function resides upon pressing the button my code comes to this function.
- (IBAction)showWeb:(id)sender {
myAppDelegate *app=[[UINavigationController alloc] initWIthRootViewController:self];
// because I want to return back to the same view
webController *web = [[webController alloc] initWithStyle:UITableViewStypeGrouped];
[app.nav pushViewController:web animated:YES];
app.nav.view.frame = CGRect(,0,320,430);
[self.view.window addSUbview:app.nav.view];
}
The web popup occurs but it is moved vertically, when I press "back button" my former view appears as well and it is also shifted vertically from what it was before.
After going back and forth few times the thing hangs.
Questions:
1. what can cause the shift?
2. how to avoid when I go "back" to see the title(test from the "back"button, I think this might cause a shift when I go back.
3. how to find out why it hangs after few attempt?
Thanks.
Victor
The line:
myAppDelegate *app=[[UINavigationController alloc] initWIthRootViewController:self];
makes no sense to me. Surely your compiler is warning you about that? What is "myAppDelegate" defined as? Classes should have a capital letter at the front, by the way.
Also, the line
[self.view.window addSUbview:app.nav.view];
is highly suspect, because the UIWindow for your application should have only one child UIView. You shouldn't just add more views willy nilly and expect things to work. It is possible to change the child UIView by removing the old one and adding a new one, but you don't seem to be doing that. Having more than one child UIView of UIWindow gets you into trouble very quickly -- for example, device orientation changing can break.
I'm not exactly clear as to why the app delegate (or the window for that matter) needs to be messed with at all to do what you are trying to do. Sounds like you should just be using standard Nav View Controllers and Web Views.
Also, you are alloc init'ing w/o any memory management.

How do I create a UINavigationController with a default "back" state?

I have a UINavigationController, complete with table view and associated magic.
The data I'm populating that table view from may have items from multiple categories, but the default view for the user will be one in which they are viewing all of the items, and then they have the ability to move backwards to a different table view that would allow them to select a different category, which would then return to the original table view with the appropriate data populated.
What's the proper approach for this? I can't seem to wrap my head around how I would make the navigation controller give me a back button (with appropriately wired up actions) without having come from a previous view in the stack (which wouldn't really exist at launch time if I start the user off from what is essentially the detail view, in stack terms.)
Also, the back button should be titled "Groups", not "Back", but that's really just an implementation detail. :)
Update: This issue finally manifested itself in production code, and here’s how I fixed it:
My UINavigationController is created in a nib, with the root view set as the “groups” view. Then, in my app delegate, I push the second view onto the stack while the app is launching.
That works fine for achieving the proper stack, but that doesn’t help with the back button title, because the navigation controller didn’t seem to want to grab the title from the root view, and instead was showing a back button with “Item” as the title.
So, on the pushed view, in viewDidLoad, I set:
self.navigationController.navigationBar.backItem.title = #"Groups";
and that did the trick.
The only potential downside of doing it this way would be if the pushed view controller were ever used in a scenario where the view below it wasn’t the groups view, but since the design of this particular application ensures that never happens, I’m accepting that failure. ;)
Another update:
I’m an idiot. Just set the title property of the navigationItem provided by the navigationController in Interface Builder, and boom, no issue. Or do it in code. It doesn’t matter, just don’t do it by setting the backItem.title way I show you above. That’s just dumb.
In your application delegate's .m file in the application:didFinishLaunchingWithOptions: method just push your view controllers like you normally would with[self.navigationController pushViewController:your_view_controller animated:YES]; and it should push them on before the application's first view controller appears.
To change the text of the button to Groups just call this before pushing your controllers.:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Groups" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];