Ios 从方法外获取价值
我有一个具有TableView的ViewController,我想从didSelectRowAtIndexPath中获取值,以便传递到另一个视图 方法如下:Ios 从方法外获取价值,ios,return-value,didselectrowatindexpath,Ios,Return Value,Didselectrowatindexpath,我有一个具有TableView的ViewController,我想从didSelectRowAtIndexPath中获取值,以便传递到另一个视图 方法如下: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // That's the string that I want to get the value NSString *select
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// That's the string that I want to get the value
NSString *selectRow = [_vinhoArray objectAtIndex:indexPath.row];
stringTest = selectRow;
}
我创建并合成属性stringTest
@property (nonatomic, strong) NSString *stringTest;
@synthesize stringTest;
要指定中的值,请选择Row,但我猜我做错了什么。因为这不起作用
有人能帮我吗?谢谢首先,我将尝试查看您是否正在通过nslog分配值
NSLog(@"%@", [_vinhoArray objectAtIndex:indexPath.row]);
如果你在日志中找到了什么,你可以试试
self.stringTest = [_vinhoArray objectAtIndex:indexPath.row];
如果要在数组中存储NSString
试试这个
在AppDelegate.h中创建字符串
@property (nonatomic, retain) NSString *myTitleString;
现在,无论您想在哪里访问这个字符串,都需要在顶部包含appDelegate
#import "AppDelegate.h"
然后用这个来访问它
AppDelegate *AppDelegate = [[UIApplication sharedApplication] delegate];
分配给
AppDelegate.MyTitleString = //whatever you are going to assign it with;
在另一个视图控制器中,您将执行相同的操作,导入顶部的appdelegat.h,创建app委托,并将其值赋予标题
NSString *MyStringInTheDifferentController = AppDelegate.MyTitleString;
在
@test之后插入以下方法代码>:
- (void)setStringTest:(NSString *)stringTest
{
_stringTest = stringTest; // Insert a breakpoint here
/* You should see here what value does stringTest have and also who called this setter. */
}
我认为这是最好的调试方法,因为您可能会在tableView:didselectRowatingIndexPath
之后覆盖stringTest
属性,而您可能不知道它
注意:如果您不使用ARC,则需要更新该设置器。那么是stringTest
还是stringTeste
?@sha stringTest!抱歉,键入错误。我在这里尝试,是的,我为stringTest
分配了一个值,但只在方法内部工作,如果我将NSLog放在方法外部,我会得到一个Null
值。我想在方法外部使用,以便可以传递到另一个视图。“self.stringTest=[\u vinhoArray objectAtIndex:indexPath.row];”应该在方法内部工作。确保NSLog语句不会在该方法之外工作,因为它使用该方法提供的indexPath。您是否尝试将其分配为“self.stringTest”?是的,如果一个putself.stringTest
我得到了该值。但仅限于方法内部。我怎么能用这个方法以外的?所以我可以在另一个ViewController中使用它?一旦从方法内部分配了它,我不明白为什么在类的其他地方调出已经分配的变量时会出现问题。如果需要将变量传递给另一个视图/类,则需要将其存储在其他位置,例如在singleton或应用程序委托中。我将尝试向您解释为什么要使用此变量。我想根据此值分配NavigationController的标题。当用户在一行中单击时,另一个viewcontroller将加载其他表,并且我的NavigationController i的标题与我的select行的标题相同。如果使用此方法,则会出现错误使用未声明的标识符\u stringTest;你是说“StringTest”吗?
我使用ARC,我正在处理XCode 5 IOS7,请从@synthetic StringTest更新代码>到@synthesis-stringTest=_-stringTest
;好的,现在是正确的。而stringTest
得到了正确的值。但是如何在另一个ViewController中使用此值?如何传递此值并在其他ViewController中使用?someOtherViewController.someProperty=self.stringTest;但是在哪里呢?对不起,老兄,这方面我是新手。如果我把你说的密码放进去-(void)setString Teste:(NSString*)stringTeste{{u stringTeste=stringTeste;//SubTabela是我的OtherViewController SubTabela*subVC=[[SubTabela alloc]init];subVC.tituloTabela=self.stringTeste;}
但是如果我在OtherViewController中放入NSLog
,在viewDidLoad
中,我得到一个null
值