- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"StoryViewController"];
HistoryDetails *obj = [[HistoryDetails alloc]init];
obj.currentIndex = indexPath.row;
[self presentViewController:storyViewController animated:YES completion:nil];
}
Here is my TableViewController's didSelectRowAtIndexPath and with the selection of cell it will go to another viewController which is HistoryDetails.
Now I have a property currentIndex inHistoryDetails class and I set data to that property which is indexPath.row. But in HistoryDetails.m I am not getting the selected index value.(Always getting 0). where is fault in my code.
Probably you mean
HistoryDetails *storyViewController = (HistoryDetails *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"StoryViewController"];
storyViewController.currentIndex = indexPath.row;
[self presentViewController:storyViewController animated:YES completion:nil];
instantiateViewControllerWithIdentifierreturns the requested view controller.
You need to cast the instantiated controller to the custom subclass.
Related
I am new to IOS Objective C Programming. I am trying a very simple app. My main page is standard View Controller where i have a simple UITableView. What i am trying to do is to make a different view controller to come up when user select different cell on the Table View. Currently i populate 3 simple lines. I have searched for many historical discussion but can't seem to work (from segue options to simple instantiate view options). I give a snip of my code and hope that anyone can point me what i am doing wrong here.
Storyboard
Ensure one are you embed with NavigationController or Not, if Not,
just do the following way
XCode menu --> Editor --> EmbedIn --> Navigation Controller, do like
you get the output of
Step-2
In here we can navigate with two types
Connection Through
Connection less
Connection Through
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SegName = #"";
if (indexPath.row == 0)
{
SegName = #"firstSegumeName";
}
else if (indexPath.row == 1)
{
SegName = #"secondSegumeName";
}
else if (indexPath.row == 2)
{
SegName = #"thirdSegumeName";
}
[self performSegueWithIdentifier:SegName sender:self];
}
Connection Less
if use `storyboard ID` with connection less
for example
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
if (indexPath.row == 0)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourfirstIdentifierName"];
}
else if (indexPath.row == 1)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yoursecondIdentifierName"];
}
else if (indexPath.row == 2)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourthirdIdentifierName"];
}
[self.navigationController pushViewController:otherViewCon animated:YES];
}
Create an array with segue names in the order in which you want to refer to the view controllers from your tableview and then in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
you can call
[self performSegueWithIdentifier:[array valueAtIndex:indexPath.row sender:nil];
If its through XIB :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:#"Your_ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
If its through StoryBoard :
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_storyboard" bundle:[NSBundle mainBundle]];
Your_ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"storyboardID"];
[[self navigationController] pushViewController:vc animated:YES];
If its through Coding :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
For Your questions :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Put above code here according to your requirement....
}
Try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstViewController *u = [[ContactDetailsVC alloc]init];
u= [self.storyboard instantiateViewControllerWithIdentifier:#"firstView"];
[self.navigationController pushViewController:u animated:YES];
}
Replace last 2 lines of didSelectRowAtIndexPath with:
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:myView];
[self presentViewController:navC animated:YES completion:nil];
I have a navigation controller, a view controller, and a table view that were all created in my storyboard:
When you touch a certain row in the table view, it SHOULD push a new controller onto the navigation controller, but instead nothing happens. Here is the code:
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *controller = (UINavigationController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"dbNav"];
SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
[controller pushViewController: slController animated: YES];
}
Try
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UINavigationController *controller = self.navigationController;
SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
[controller pushViewController: slController animated: YES];
}
Basically instantiateViewControllerWithIdentifier creates a new instance of the specified view controller each time you call it, so you are pushing the view in a different Navigation controller than the one that is displayed
This worked for me. You have to define index path.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ServicesModel *service = [services objectAtIndex:indexPath.row];
ServiceViewController *serviceViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ServiceView"];
serviceViewController.serviceModel = service;
[self.serviceController pushViewController:serviceViewController animated:YES];
}
I have multiple cells in a table referring to the same DetailViewController. How can I use nibs to be redirected to the same DetailViewControllers with different content?
You can get the selected indexPath by this method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
From this method you can load the DetailViewController by this,
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *tableViewNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.navigationController presentViewController:tableViewNavigationController animated:YES completion:nil];
In the above method you can assign values what you want to change when you select different cells.
I was trying to pass data between an UITableViewController which is showed with:
SeleccionBundesligaViewController *seleccionBundesligaVC = [[SeleccionBundesligaViewController alloc]initWithStyle:UITableViewStylePlain];
seleccionBundesligaVC.title = #"1.Bundesliga";
[self.navigationController pushViewController:seleccionBundesligaVC animated:YES];
and when I select a row it's closed by:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [equipos objectAtIndex:indexPath.row];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
InformacionPartidaViewController *infoPartidaVC = [storyboard instantiateViewControllerWithIdentifier:#"infoPartidaVC"];
[self.navigationController popViewControllerAnimated:YES];
[infoPartidaVC.labelNombreEquipo setText:[dic objectForKey:#"nombre"]];
}
But when I select the row, it doesn't pass data to 'infoPartidaVC'.
Your best bet is to create a property in your SeleccionBundesligaViewController's .h file:
#property (weak, nonatomic) InformacionPartidaViewController *infoPartidaVC;
When you create your UITableViewController and before you push it, add the reference:
SeleccionBundesligaViewController *seleccionBundesligaVC = [[SeleccionBundesligaViewController alloc]initWithStyle:UITableViewStylePlain];
seleccionBundesligaVC.title = #"1.Bundesliga";
seleccionBundesligaVC.infoPartidaVC = self;
[self.navigationController pushViewController:seleccionBundesligaVC animated:YES];
Then, in your didSelectRowAtIndexPath in your UITableViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [equipos objectAtIndex:indexPath.row];
[self.navigationController popViewControllerAnimated:YES];
[self.infoPartidaVC.labelNombreEquipo setText:[dic objectForKey:#"nombre"]];
}
I think your problem is that you are setting the text of the label before the label is shown.
have a look at this question asked answered by me
whats the best way, to init a DetailView from a TableView with data?
I need to "send" a object to fill all the labels and ImageViews.
My Code for changing the view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"detailView"] animated:true];
}
Maik
DetailViewController *viewController = (DetailViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
viewController.myProperty1 = object1;
viewController.myProperty2 = object2;
[self.navigationController pushViewController:viewController animated:YES];
Just take the ViewController instantiated from the storyboard and set it up before you push the ViewController
YourViewControllerclass * vc = (YourViewControllerclass*) [self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
[vc setupMethodForObject: object];
[self.navigationController pushViewController:vc animated:YES];