-1

我在 Mainclass 中有一个 UILabel,现在我想通过 Subclass 函数为其分配文本。我的问题详细代码可在此处链接现在我希望当我单击 UITableview 的任何单元格(这是我的子类)时,其文本分配给 Mainview 中的 UILabel。我的代码低于我尝试过的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSLog(@"MY row Text:%@",cellText);

// Now here I don't know how we can pass this text to UILabel which is in my mainclass

}

我的下图也有助于理解我的问题

在此处输入图像描述

4

2 回答 2

1

将标签的引用传递给子类。然后子类可以简单地设置标签的文本。

在表视图控制器中添加属性 UILabel *mainLabel。然后将此属性设置为来自主类的标签引用。

于 2012-11-10T15:20:52.247 回答
1

您可以在创建子视图类时将标签传递给它。

在主视图类中声明:

@property (nonatomic, strong) UILabel *mainLabel;
// or if no ARC
// @property (nonatomic, retain) UILabel *mainLabel;

然后在子视图的 .h 上,声明:

@property (nonatomic, weak) UILabel *myAccessToMainLabel;
// or if no ARC
// @property (nonatomic, assign) UILabel *myAccessToMainLabel;

创建子视图时,将主视图的标签分配给子视图的 myAccessToMainLabel。然后在子视图代码中分配标签。你总是可以在 Objective-c 中传递属性。

编辑:当您创建子视图时,将 mainLabel 从创建它的代码中分配给子视图的引用 iVar。例如,如果主创建子视图,则:

MySubViewClass *mySubView = [[MySubViewClass alloc] initWithNib:@"MySubViewClass" ...];
// or any variety of init
mySubview.myAccessToMainLabel = mainLabel;
于 2012-11-10T15:24:40.213 回答