2

I have RootCtrl & DetailCtrl. On RootCtrl, I have a uiTableview. I use this function to write the result of the choice :

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",cell.textLabel.text);

into didSelectRowAtIndexPath. I'd like to display the NSLog result in UILabel into Detailctrl, so an another view. How I can do it ?

Tom John
  • 99
  • 1
  • 7

3 Answers3

1

Input the code below in the RootViewController:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString*yourStringNameHere = [NSString stringWithFormat:@"%@",cell.textLabel.text];
NSLog(@"%@",yourStringNameHere);//this isn't used in retreiving the info
NSNumber *number = [NSNumber numberWithInt:1];
NSDictionary *yourDictionaryNameHere = [NSDictionary dictionaryWithObjectsAndKeys:
                    yourStringNameHere, @"yourStringNameHereDictionary",nil];

Input the code below in the DetailViewController:

In the header file(.h) put

IBOutlet UILabel *yourLabelNameHere; 

In the main file(.m) use

yourLabelNameHere.text= [yourDictionaryNameHere objectForKey:(@"yourStringNameHereDictionary")];

Notes:

  • If you can't have access the dictionary put #import "RootViewController.h"
  • We use the NSDictionary to store data on the iPhone memory, this allows us to use the data from the other class, otherwise with just using #import you only receive the data from the initialization of the variable.
  • Receive a string from the NSDictionary from a key
  • Set up a UILabel in the DetailViewController.h that outputs to a storyboard or xib file
  • For loading the label at the start of the transition, put the dictionary receiver in ViewDidLoad or a method(-(void)function) called by ViewDidLoad
SkylerHill-Sky
  • 2,106
  • 2
  • 17
  • 33
0

You have to set a @property UILabel* myLabel in your DetailCtrl class . Then, use

MyDetailCtrlInstance.myLabel.text = cell.textLabel.text
dreamzor
  • 5,795
  • 4
  • 41
  • 61
  • hi, I tried. I have 2 Issues. "Use of undeclared identifier 'cell'" – Tom John Sep 23 '12 at 20:53
  • And Property 'myLabel' not found on object of type 'DetailCtrl' but it is present... – Tom John Sep 23 '12 at 20:54
  • ...did you add it to your class? – dreamzor Sep 23 '12 at 20:56
  • this doesn't work since he's trying to access the data from another class. – SkylerHill-Sky Sep 23 '12 at 22:35
  • I thought this might work, if he imported it in RootCtrl properly, and used the line dreamzor wrote above, in the proper method. I mean, there's no reason why if he puts it in the right method that cell would be undeclared identifier to begin with, right? – minjiera Sep 23 '12 at 23:30
  • @minijiera No, when you move to the new class, all the data will be erased unless stored with core-data or the NSDictionary, he didn't say its a modal, but that he's calling a different class. – SkylerHill-Sky Sep 23 '12 at 23:42
0

Ok, first of all, forget about the NSLog output. NSLog is NSLog. That's something else. What you really want is to get the text of the cell, store it somewhere, retrieve it in another class and display it. So what I would do is to create extern variable (a NSString) that store the freshly selected cell text in didselectrowatindexpath, and access it in Detailctrl.

Not sure if everyone will do it this way but that's how I would do it and it should work.

minjiera
  • 336
  • 3
  • 16