在UILabel xcode中显示NSLog结果



我有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中访问它。

不确定是否每个人都会这样做,但这就是我会这样做的方式,它应该有效。

最新更新