问题
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 ?
回答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 byViewDidLoad
回答2:
You have to set a @property UILabel* myLabel
in your DetailCtrl
class . Then, use
MyDetailCtrlInstance.myLabel.text = cell.textLabel.text
回答3:
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.
来源:https://stackoverflow.com/questions/12556046/display-nslog-result-in-uilabel-xcode