我有RootCtrl和DetailCtrl。在RootCtrl上,我有一个uiTableview。我用这个函数来写选择的结果:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell.textLabel.text);
into didSelectRowAtIndexPath. 我想在 Detailctrl 中显示 UILabel 中的 NSLog 结果,所以另一个视图。我该怎么做?
在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];
在DetailViewController
中输入以下代码:
在头文件(.h)中放入
IBOutlet UILabel *yourLabelNameHere;
在主文件(.m)中使用
yourLabelNameHere.text= [yourDictionaryNameHere objectForKey:(@"yourStringNameHereDictionary")];
笔记:
- 如果您无法访问字典,请输入 #import"RootViewController.h"
- 我们使用 NSDictionary 将数据存储在 iPhone 内存上,这允许我们使用来自其他类的数据,否则仅使用 #import 您只能从变量初始化中接收数据。
- 从密钥接收来自 NSDictionary 的字符串 在输出到
- 情节提要或 xib 文件的
DetailViewController.h
中设置 UILabel - 要在转换开始时加载标签,请将字典接收器放入
ViewDidLoad
或由ViewDidLoad
调用的方法(-(void)function)
您必须在DetailCtrl
类中设置@property UILabel* myLabel
。然后,使用
MyDetailCtrlInstance.myLabel.text = cell.textLabel.text
好的,首先,忘记 NSLog 输出。NSLog 就是 NSLog。那是另一回事。您真正想要的是获取单元格的文本,将其存储在某个地方,在另一个类中检索它并显示它。所以我要做的是创建extern变量(NSString),将新选择的单元格文本存储在didselectrowatindexpath中,并在Detailctrl中访问它。
不确定是否每个人都会这样做,但这就是我会这样做的方式,它应该有效。