UITableViewController isn't passing data - ios

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

Related

Index value transfer between views

- (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.

Navigating to different ViewControllers from UITableView

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];

objective c - click table row and go to another page

I want to user click table row then after it user will go to other page. The page has Storyboard ID "Other View" and Restoration ID "Other View".
But I can't go to purposed view
This is my code :
- (UITableViewCell *)tableView:(UITableView *)tabel cellForRowAtIndexPath:(NSIndexPath *)indeksBaris
{
static NSString *simpleTableIdentifier = #"TampilanCell";
TabelBeritaCell *cell = (TabelBeritaCell *)[tabel dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSLog(#"nilai : %d", indeksBaris.row );
//detecting NIB of table view
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableView" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Go To View Controller which have identifier "Other View"
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
cell.judulBerita.text = [listBerita objectAtIndex:indeksBaris.row];
return cell;
}
This code doesn't work :
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
UPDATED
I have insert new code. I use didSelectRowAtIndexPath method but it doesn't work :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
}
Try the method didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"ShowDetail" sender:tableView];
}
If you want to pass a variable or perform an action before it segues, do the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ShowDetail"]) {
//Do something
Detail *detailController = (Detail*)segue.destinationViewController;
}
}
If you want to know how to name a segue, here is a great tutorial from apple docs: Naming Segues
Your code seems to be correct. just i feel problem in finding UIStoryboard object .replace following things may this work fine.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"your_storyboard_name" bundle:nil];// like Main.storyboard for used "Main"
otherViewCon = [storyboard instantiateViewControllerWithIdentifier:#"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
}
You should write navigation code in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
this method.
Or you can directly go to another view controller by drawing push segue in storyboard.No need to write code.
How to make a push segue when a UITableViewCell is selected
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * storyboardIdentifier = #"storyboardName";// for example "Main.storyboard" then you have to take only "Main"
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: [NSBundle mainBundle]];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:#"secondVCStoryboardID"];
[self presentViewController:UIVC animated:YES completion:nil];
}
Try this its help you. didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row==0)//means you click row 0
{
UIViewController *otherView=[self.storyboard instantiateViewControllerWithIdentifier:#"otherview"];
[self presentViewController:otherView animated:YES completion:nil];
}
else
{
UIViewController *detailView=[self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
[self presentViewController:detailView animated:YES completion:nil];
}
}
in here showDataVC is a my new file name for second ViewController.
in a first created a object of new file and initialize.
and initialize showDataVC with object to use object in pushviewcontroller.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:#"showDataVc"];
[self.navigationController pushViewController:objshowDataVc animated:YES];
}
Implement your code in didSelectRowAtIndexPath method. And please read the documentations before writing code n research before you put questions here.

How to pass data using didSelectRow in UISplitViewController - from master to detail

I want to pass some core data from master to detail.
First the user selects a name which has a gender assigned. If gender Girl "pige" it goes to the girls UITableiew. From there each selection shows on detail view.
I want to pass some data from the girl UITableiew. Like if its a girl color it pink. set a title etc.
So far i know how to pass around the UITableiew "master" but not how to get it to the detail view
Here's some code on how i manage the selection of UITableView etc.
DetailViewContainerController
UIStoryboard* initial = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
self.initital = [initial instantiateViewControllerWithIdentifier:#"initialSite"];
[self addChildViewController:self.initital];
self.startSide = [initial instantiateViewControllerWithIdentifier:#"startSide"];
[self addChildViewController:self.startSide];
-(void)initialSite:(int)viewId
{
UIViewController *viewController;
switch (viewId)
{
case 0:
viewController = self.initital;
[self.navigationController setNavigationBarHidden:NO animated:NO];
break;
case 1:
viewController = self.startSide;
[self.navigationController setNavigationBarHidden:NO animated:NO];
break;
}
[self showChildViewController:viewController];
}
-(void)showChildViewController:(UIViewController*)content
{
if(topController != content)
{
content.view.frame = [self.view frame]; // 2
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
topController = content;
}
}
startTableViewController
-(void)viewDidLoad
{
arrayA = [[[NSMutableArray alloc] initWithArray:[Start findAllSortedBy:#"navn" ascending:YES]] mutableCopy];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"gender"]isEqualToString:#"Pige"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MenuPige"];
controller.forNavn = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"gender"]isEqualToString:#"Dreng"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MenuDreng"];
controller.forNavn = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
}
MenuPige
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.detailViewContainerController initialSite:[indexPath row]];
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"menuPunkt"]isEqualToString:#"Barnedåb / Navngiving"]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"BarneDaab" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"barneDaab"];
[self.navigationController pushViewController:controller animated:YES];
}
in the girl menu.h i have the NSStrings of the data that i want to pass. So that is working. but how to get it to detailviewcontroller ?
UPDATE:
I tried this Accessing array from a detail view controller into master view controller in Objective-C
but can't get it work
UPDATE 2:
self.navigationController.parentViewController.navigationItem.title = [self.detailViewContainerController.forNavn valueForKey:#"navn"];
doesn't work in the velkommen.m, it returns nil
UPDATE 3:
Leftmenucontroller.m - this works. but only to another uitableview. can't get it over to the detail view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewContainerController *start = [[DetailViewContainerController alloc]init];
start.forNavn = forNavn;
NSLog(#"leftview = %#", forNavn);
[self.detailViewContainerController initialSite:[indexPath row]];
}
UPDATE 4:
startTableViewController.h
{
NSMutableArray *arrayA;
NSMutableArray *tableData;
NSString *forNavn;
NSString *mellemNavn;
NSString *efterNavn;
NSString *gender;
}
#property (nonatomic, retain) NSString *forNavn;
#property (nonatomic, retain) NSString *mellemNavn;
#property (nonatomic, retain) NSString *efterNavn;
#property (nonatomic, retain) NSString *gender;
#property (nonatomic, strong) NSMutableArray *arrayA;
UPDATE 6:
I figured out that i can pass the data to DetailviewContainerController with this
DetailViewContainerController *test = (DetailViewContainerController *) [[self.splitViewController.viewControllers lastObject] topViewController];
test.leftViewController = self;
test.forNavn = forNavn;
NSLog(#"test = %#",test.forNavn);
but now i can't get it further from DetailViewContainerController. i tried this :
VelkommenViewController *test2 = (VelkommenViewController *) [[self.splitViewController.viewControllers lastObject] topViewController];
test2.detailViewContainerController = self;
test2.forNavn = self.forNavn;
NSLog(#"test2 = %#",test.forNavn);
Update 7:
Update 8:
VelkommenViewController *test2 = [[[self.splitViewController.viewControllers.lastObject topViewController] childViewControllers] objectAtIndex:0];
test2.forNavn = forNavn;
NSLog(#"test3 = %#",test2.forNavn);
Throws a error : -[UINavigationController setForNavn:]: unrecognized selector sent to instance 0x175b56c0
Update 9:
It's a little unclear what you're doing, especially with the child view controller stuff. But, in general, when working with a split view controller, the detail controller can be accessed by using self.splitViewController.viewControllers[1] (if the detail controller is embedded in a navigation controller, then that gets you a reference to the navigation controller, and you have to use its topViewController property to get to the detail controller). You shouldn't be using instantiateViewControllerWithIdentifier:, or alloc init to access the detail controller, since either of those methods will give you a new instance instead of referencing the one you have on screen.
on the receiving end this works:
NSString *name = ((DetailViewContainerController *)self.parentViewController).forNavn;

iOS: UITableView -> DetailView init with data

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];

Resources