Objective c 如何在类之间共享方法
我想将标签的文本设置为来自不同类的单元格文本 在我的MasterViewController中,我有:Objective c 如何在类之间共享方法,objective-c,ios,xcode,xcode4.5,Objective C,Ios,Xcode,Xcode4.5,我想将标签的文本设置为来自不同类的单元格文本 在我的MasterViewController中,我有: // SAVE TEXT OF SELECTED CELL - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// 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;
}
我也有这个
@interface ChemProjectMasterViewController : UITableViewController
@property (nonatomic)NSString *cellString;
@property (nonatomic)NSString *sharedCellString;
+(ChemProjectMasterViewController*) sharedVariable;
@end
现在,我要从中访问此方法的类是DetailViewController。在这方面,我有:
detailDescriptionLabel.text = [ChemProjectMasterViewController sharedVariable].sharedCellString;
其中detailDescriptionLabel只是一个文本标签
**
该程序编译得很好,但当我单击按钮时,标签文本被设置为更改。该应用程序运行平稳,直到我按下导致其崩溃的按钮。主要目标是将DetailViewController中的标签文本设置为MasterViewController中单元格的文本。谢谢你的帮助 我看到的东西很少 第一个,在.h中声明为类方法(+),而在.m中声明为实例(-) 第二个是
@property(非原子)NSString*sharedCellString代码>不安全且未恢复的,请使其强大
如果有效,请告诉我。冒着过于明显的风险,detailDescriptionLabel.text
需要一个字符串。您正在传入一个视图控制器。为什么?@StevenFisher-他实际上是在传递一个字符串。他从类ChemProjectMasterViewController
@jtetreault13访问名为sharedCellString
的变量的属性sharedVariable
,您在其标题中声明为+(ChemProjectMasterViewController*)sharedVariable在.m文件中,您将其定义为-(ChemProjectMasterViewController*)sharedVariable
,我不确定,但这可能会产生问题。哦,您是对的。[ChemProjectMasterViewController sharedVariable]
上的额外空格(删除后)将属性推到了右侧。我使sharedCellString变强,但它似乎没有任何作用,仍然得到了相同的SIGABRT。现在,当我进入.h并将其从类方法(+)更改为实例(-)时,它抛出一个错误,称“在'id'类型的对象上找不到属性'sharedCellString'”