【发布时间】:2012-11-29 03:37:15
【问题描述】:
我想将标签的文本设置为来自不同类的单元格的文本。
在我的 MasterViewController 我有:
// SAVE TEXT OF SELECTED CELL
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cellString = cell.textLabel.text;
}
//SHARES VARIABLES BETWEEN CLASSES
- (ChemProjectMasterViewController*) sharedVariable
{
static ChemProjectMasterViewController *myVariable = nil;
if (myVariable == nil)
{
myVariable = [[[self class] alloc] init];
myVariable.sharedCellString = cellString;
}
return myVariable;
}
我在 .h 中也有这个
@interface ChemProjectMasterViewController : UITableViewController
@property (nonatomic)NSString *cellString;
@property (nonatomic)NSString *sharedCellString;
+(ChemProjectMasterViewController*) sharedVariable;
@end
现在我要访问此方法的类是 DetailViewController。我有:
detailDescriptionLabel.text = [ChemProjectMasterViewController sharedVariable].sharedCellString;
其中 detailDescriptionLabel 只是一个文本标签。
** 该程序编译得很好,但是当我单击一个按钮时,我将标签文本设置为更改。该应用程序运行平稳,直到我按下导致它崩溃的按钮。主要目标是将 DetailViewController 中的标签文本设置为 MasterViewController 中的单元格文本。感谢您的帮助!
【问题讨论】:
-
冒着过于明显的风险,
detailDescriptionLabel.text期待一个字符串。您正在传入一个视图控制器。为什么? -
@StevenFisher - 他实际上是在传递一个字符串。他从类
ChemProjectMasterViewController访问一个名为sharedVariable的变量的一个名为sharedCellString的属性。 -
@jtetreault13 您在其标题中声明为
+(ChemProjectMasterViewController*) sharedVariable;,在.m 文件中您将其定义为-(ChemProjectMasterViewController*) sharedVariable,我不确定,但这会产生问题。 -
哦,你是对的。
[ChemProjectMasterViewController sharedVariable]上的额外空格(已删除)将属性推到了右侧。
标签: objective-c ios xcode xcode4.5