I want to implement a "Next" feature in my app. The button appears on a ViewController that is shown by clicking on a row in a UITableView. Ideally what I would like is for Next to do the following (in pseudo code)
Go "back" to the UITableView
Scroll to next available item in UITableView
Select that item
I believe I can do 2,3 via selectRowAtIndexPath (please feel free to correct me if I am wrong) but I am unsure how to do Step 1. I will also have to target a method in the UITableView rather than the UIViewController that hosts the Button. How would I achieve this?
Update:
I can now target the rootViewController but now do I actually show it? (For reference here is my code, thanks to this question)
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:rootViewController action:#selector(nextButton:)];
Update 2
I now have the back animation working but it keeps the navigation bar intact, how do I also set this back to the correct (rootViewController) one?
[self.navigationController popToRootViewControllerAnimated:YES];
If you have situation like this:
RootViewController - tableView
NextviewController - some view with Next button which shows after the you click on tableView
When you issue [self.navigationController popToRootViewControllerAnimated:YES]; from the 2. controller by pressing the Next button, it will bring RootViewController to the screen.
Now, on the RootViewController you have to "detect" back action (perhaps in ViewWillAppear) and continue with your steps 2. and 3.
Related
I'm trying to build a reusable code that I will be able to use in several places in my IOS application.
What it does is to create a table that each cell in it has several UIButtons. When clicking on a button, the View needs to be redirected to another ViewController and show data related to that button (user ID for example).
Because I want this code to be reusable, I've created a UIViewController that the other views will inherit from. My ViewController programmatically builds the UITableView and cells and populates everything inside.
The only thing I can't figure out is how to redirect the view to the next when touching the UIButton. I'm trying to do that without segues because I want this as a self sustained component and without the need to 'play' with storyboards and push the same segues repeatedly in different places.
Any suggestion will be appreciated!
Use this code when hitting the button
ViewController *obj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
Where ViewController is the controller you are going to.
If you want to pass data just set a variable in the ViewController your going to on the button click. Use the code above with the line below before the push
obj.userId = #"UserId1";
I have a Navigation-Based app in which i added ABPeoplePickerNavigationController as a subview to my Navigation Contorller like this:
I have saved the view before adding subview into navView.
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
navView = [[UIView alloc] init];
navView = [[self.view superview] superview];
[[[self.view superview] superview] addSubview:[peoplePicker view]];
It works fine, but when I'm done with the PeoplePicker and I want to get back to the previous view. I used this code but it doesn't work.
[[[self.view superview] superview] addSubview:navView];
I don't get it, I have saved the navView from the subview, now I can't bring it back?
You need to add the back button, like so:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
Ideally your NavigationController is the root of your application, in which case any other view is a subview of it so you never trully swich away from your navigation controller, you merely change the actual view that is shown.
Also the way in which you attempt to reference your first view is fairly messy/confusing. You should change it to something like:
[appDelegate.navigationController topViewController];
or
[appDelegate.navigationController popToRootViewController];
I cant tell you for certain which to use as I am not sure of your exact view hierarchy, but I would highly recommend that you try to optimize that code.
I should probably note that the back button on the navigation controller and the the two lines below that do different things. The back button returns you one view back, while popToRoot and topView are both intended to bring you back to the very first viewController.
Edit: Changed the back button code to the code I actually use
Edit2: Just because I have a bit more space here I can try to understand your heirarchy better. It looks something like this?
Root View or Root View
-> Group View -> Group View
ABPeoplePicker View -> ABPeople Picker View
Root View
-> Group View
-> -> ABPeoplePicker
Meaning that ABPeoplePicker is nested within Group, and that is nested within your root? I think that its not working because what you actually have is one of the two on the first line, when what you want is the second line. Because I don't actually know which one you have without seeing it, I cant tell you exactly what to change, but if you want it to be nested all the way like the heirarchy on the second line, you need to push your group view onto the root view controller(Which is your navigation controller), then you want to push People Picker onto the root view controller again. After this your back button and view switching should be working the way you want it to.
I have a Navigation Controller in my app delegate that i am using to switch between views and that seems to work fine clicking the back button in the NavigationBar, but only when i have a UITable in the mix. But when i pushRootviewController to a standard root view controller i still see the back button in the UINavigationBar but when i click it the program quits and logs no errors.
I thought maybe something like this would work, but no luck.
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIBarButtonItem *iButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(playThis:)];
appDelegate.navigationController.navigationItem.backBarButtonItem = iButton;
[iButton release];
Anyone have something similar happen? Any ideas?
Thanks!
Your problem is that you set the button to the navigation controller itself. you should be setting the button to the navigationItems of your Controllers inside your Navigation Controller (and remember that the backBarButtonItem is displayed on the navigation bar when the next (follow-up) view controller is visible. if that is not the behaviour you want, you should use leftBarButtonItem.) also remember that "back buttons" should have target/action set to nil as the navigation controller adds itself als target when you use it as backBarButtonItem.
I m making an app which has a main TableView. when we click any cell we got DetailView of that cell. we can also change DetailView without going back to main TableView by using next and prev button on every DetailView. When i click prev button. I get previous item'd DetailView but animation is like its going forward. i m using this:
[self.navigationController pushViewController:prevView animated:YES];
can anybody tell me how can i change that animation like Back button animation.
Thanx in advance
You have to store your navigationController, then 'pop' your current view with an animation and 'push' the detail view without an animation, for example like Squeegy did here. I've adjusted Squeegy's code a bit, the following should work:
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
// Pop this controller and replace with another
[navController popViewControllerAnimated:YES];
[navController pushViewController:prevView animated:NO];
I have two tabs in my app, each of them is a UITableView, and each of the views in the two tabs has its own DetailViewController.
Now, if I click on a TableViewCell in the DetailViewController in the first tab, I want to jump to the DetailViewController of the second tab. I know how to access the second tab
self.tabBarController.selectedIndex = 1;
and I know how to access the DetailViewController, but only without jumping to the second tab.
Is it possible to access the second tab, and then access its DetailViewController?
It would be best if the main TableView in the second tab wouldn't be visible at all, so, it should jump directly to the DetailViewController of the second tab, with the navigation controller showing the "back" button to the main view controller and the second tab highlighted. Is this possible? And, if it is, how can I do this?
Thanks in advance :-)
The tabBarController has an array with the viewController of every tab. You can push the DetailViewController like this:
[[self.tabBarController.viewControllers objectAtIndex:1] pushViewController:detailViewController animated:NO];
Before that you might want to pop to the rootViewController:
[[self.tabBarController.viewControllers objectAtIndex:1] popToRootViewControllerAnimated:NO];