Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在类之间共享方法_Objective C_Ios_Xcode_Xcode4.5 - Fatal编程技术网

Objective c 如何在类之间共享方法

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];

我想将标签的文本设置为来自不同类的单元格文本

在我的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;
}
我也有这个

@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-(ChemProjectMasterViewController*)sharedVariable
,我不确定,但这可能会产生问题。哦,您是对的。
[ChemProjectMasterViewController sharedVariable]
上的额外空格(删除后)将属性推到了右侧。我使sharedCellString变强,但它似乎没有任何作用,仍然得到了相同的SIGABRT。现在,当我进入.h并将其从类方法(+)更改为实例(-)时,它抛出一个错误,称“在'id'类型的对象上找不到属性'sharedCellString'”