I'm trying to create a small app in iOS. After the Login Page, I have a TabBarController and the first tab in it is a TableViewController. In didSelectRowAtIndexPath I'm trying to push one view controller for every selected row. The same view controller but self.navigationController is (null) when I print it using an NSLog and I'm not able to push the ViewController. HELP!!
Here's the sample Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here, for example:
// Create the next view controller.
PlayerDetailsViewController *detailViewController = [[PlayerDetailsViewController alloc] initWithNibName:#"PlayerDetailsViewController" bundle:nil];
detailViewController.myImg.image=[UIImage imageNamed:[self.arrNames objectAtIndex:indexPath.row]];
detailViewController.name.text=[self.names objectAtIndex:indexPath.row];
detailViewController.year.text=[self.draft objectAtIndex:indexPath.row];
detailViewController.height.text=[self.height objectAtIndex:indexPath.row];
detailViewController.weight.text=[self.weight objectAtIndex:indexPath.row];
detailViewController.pro.text=[self.pro objectAtIndex:indexPath.row];
detailViewController.ppg.text=[self.ppg objectAtIndex:indexPath.row];
detailViewController.apg.text=[self.apg objectAtIndex:indexPath.row];
detailViewController.rpg.text=[self.rpg objectAtIndex:indexPath.row];
NSLog(#"%#",self.navigationController);
[self.navigationController pushViewController:detailViewController animated:YES];
}
UPDATE: I got the navigationController to work but now, the data in the pushed view(PlayerDetails) in blank!!
It's just wrong that you haven't nested your firstViewController inside UINavigationController.
So I guess your code will be seems like this when creating UITabViewController.
[tabVC setViewControllers:[vc1,vc2,nil]];//tabVC = UITabViewController
And it need to be changed something like below;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc1];
[tabVC setViewControllers:[nav,vc2,nil]];
Assume that the instance of UITabBarController is tabVC and the instance of UITableViewController is tableVC.
UINavigationController *nav = [[UINavigationController alloc initWithRootViewController:tabVC];
or
UINavigationController *nav = [[UINavigationController alloc initWithRootViewController:tableVC];
[tabVC setViewControllers:#[nav]];
Both will fix your problem.
Related
For pushing a view controller I'm using following code. On tap of UITableViewCell need to push DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row)
{
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
}
}
Now problem is I get a jerk while animating. Refer to screenshot
its because of transparency. just set a background color for your viewcontroller. thats all.
Most probably you're doing something "heavy" on the main thread, while loading DetailViewController.
Could you show us what are you doing in DetailViewController?
I think there is issue with allocating object and pushing same object.
Do the following changes, it will work fine.
Your code :
DetailViewController *controller = [DetailViewController new];
[self.navigationController pushViewController:controller animated:YES];
Replace :
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
MyViewController *controller = (MyViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"<your Storyboard ID>"];
it will work fine at my end.
Hope this help you.
I'm having a bit of a problem with my app, I have these view controllers below:
I have the locations view controller with an array of locations and I have a locations array in the Menu Table view controller, I need to pass the array from Locations to Menu Table, but I dont know how, can somebody Help me? I have tried this coding below:
-(void)pushMenuTableView
{
UIStoryboard *storyboard = self.storyboard;
UINavigationController *menuTableController = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewController"];
MenuTableViewController *menuController = (MenuTableViewController *) menuTableController.topViewController;
[self.navigationController pushViewController:menuTableController animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuTableViewController *mvc = [[MenuTableViewController alloc]init];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Location *selectedLocation = [_locations objectAtIndex:[path row]];
[mvc setLocation:selectedLocation];
[self pushMenuTableView];
}
You are using two different MenuTableViewController objects. One you create using alloc/init and give the Location to and another that you obtain from the navigation controller and then push. You need to pass the data to the controller you're going to display instead of one you make temporarily.
How about using only one navigation controller and moving through the view controllers using segues?
You could try something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewController"];
Location *selectedLocation = [_locations objectAtIndex:indexPath.row];
vc.location = selectedLocation;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nc animated:YES completion:nil];
}
In my app I am using the following didSelectRowAtIndexPath method to go to a viewController, but I am working within an story Board, and an exception is thrown telling me that the nib file is not found. How should I change the method to open the new viewController without exceptions?:
//link to the next view
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
EmpresasTableViewController *detailViewController = [[EmpresasTableViewController alloc] initWithNibName:#"EmpresasTableViewController" bundle:nil];
detailViewController.title =[[categorias objectAtIndex:indexPath.row]objectForKey:#"idCategoria"];
detailViewController.categoriaDescription = [categorias objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
you have to make an identifier (detailViewController for example)for you ViewController in your storyboard and then instantiate like this your EmpresasTableViewController
EmpresasTableViewController *detailViewController =[self.storyboard instantiateViewControllerWithIdentifier:#"detailViewController"];
i hope that help you
Basically, I have two MasterViewControllers for a splitViewController,but I am only able to run the app with one of them at a time. Both MasterViewControllers are using the same DetailViewController.
When the user push a tabbarbutton, the second masterViewController is pushed.
The splitViewController is straight forward declared like this in the AppDelegate
MasterViewControllerOne *mvc1 = [[MasterViewControllerOne alloc] initWithNibName:#"MasterViewControllerOne_iPad" bundle:nil];
UINavigationController *masterOneNavigationController = [[UINavigationController alloc] initWithRootViewController:mvc1];
DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:dvc];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = #[masterOneNavigationController, detailNavigationController];
self.window.rootViewController = self.splitViewController;
During runtime, I want MasterViewControllerTwo to swap with MasterViewControllerOne
Action to push the new view controller:
MasterViewControllerTwo*mvc2=[[MasterViewControllerTwo alloc]initWithNibName:#"MasterViewControllerTwo_iPad" bundle:nil];
[self.navigationController pushViewController:mvc2 animated:YES];
This swaps in the mvc2 on top of mvc1. However, tapping the rows of the tableview in mvc2 does not activate the DetailViewController.
To activate the DetailView (identical for mvc1 and mvc2) I do following
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Item *theItem =[[self.items objectAtIndex:[indexPath section]]objectAtIndex:[indexPath row]];
[self.detailViewController setLabelsForItems:theItem];
}
Works very well as long the MasterViewController is defined in the appdelegate as member of the splitviewcontrollers array. So why doesn't the mvc2 activate the DetailViewController when it is on top of the stack?
tapping the rows of the tableview in mvc2 does not activate the
DetailViewController
Well, that isn't going to happen magically all by itself. It's up to you to respond when a row of the second table view is selected. You haven't shown any code for doing that, so one assumes you have none.
In other words, a split view controller does nothing in and of itself except for containing the master and the detail. Communicating between the master and the detail is up to you. The template shows you the sort of thing you'll need to do.
I have a
UIViewController-> UINavigationBar + UITableView
Just a bit more explanation
I made it through UIBuilder..
1: Created a New UIViewController with XIB-file
2: Using UIBuiler i put a UINavigationController
3: Then i put UITableView underneath the navigationBar
so it gave me..
A: UIViewController-> UINavigationBar + UITableView
Now i am loading the data in UITableView from a Webservice which is working fine.
I again made a xib with sam config which is
B: UIViewController-> UINavigationBar + UITableView
So now when i try to push view B on view A using below code...it wont at all work...
SelectSiteViewController *siteViewController = [[SelectSiteViewController alloc] initWithNibName:#"SelectSiteViewController" bundle:nil];
[self.navigationController pushViewController:siteViewController animated:YES];
When i checked the UINavigationController *nav = self.navigation
nav is 0x0 that is i assume NIL.
Can anybody tell me whats wrong in here.. why is it nil...and how can i make it work..
Thanks a Lot....I would really appreciate any help
In UIBuilder verify that UINavigationController is referenced by the File's owner.
Got it.
I changed the Architecture a little bit.
I made a New UIViewController class with its xib. And coded new UINavigationController.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *navigationController
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
switch (whichViewController) {
case 1:
viewController = [[xxxx alloc] init];
break;
case 2:
viewController = [[xxx1 alloc] init];
break;
default:
break;
}
[navigationController pushViewController:viewController animated:NO];
[viewController release];
}
And pushing the view in the Switch Statement....
I hope this makes sense ......
Thanks jamihash
So you are adding the tableview to the navigation controller right? This is how:
tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tableView animated:NO];
The tableview gets added as the rootview to the navigation controller. And then on selecting a row if you wish to push another viewcontroller use the
self.navigationController pushViewController: newViewController animated:YES];
inside the didSelectRowAtIndex method.
NOTE: its UITableViewController *tableView and UINavigationController *navigationController by declaration. So code accordingly for your table.
why UIViewController-> UINavigationBar + UITableView ?
I suggest you another approach
->UITableViewController A -> Embedded with a Navigation Controller, then after populate tableview you can pass data to
->UITableViewController B by
[[self storyboard]instantiateViewControllerWithIdentifier:#"ControllerB"];
then, in storyboard, drop a tableView B and in Identity->storyboard Id put some id.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sUrl= #"<#string#>";
if (indexPath.row == 0) sUrl= #"http://www.apple.com/";
if (indexPath.row == 1) sUrl= #"http://www.google.com/";
if (indexPath.row == 2) sUrl= #"http://www.times.uk/";
NSMutableArray *selectedObject = [arrayOpenMale objectAtIndex:0];
NSLog(#"%# is the selected object.",selectedObject);
SeriesAdetailVC *dvc = [[self storyboard]instantiateViewControllerWithIdentifier:#"DetailView"];
dvc.strings7 = [NSURL URLWithString:sUrl];
[self.navigationController pushViewController:dvc animated:YES];
}
hope help