简体   繁体   English

通过方法获得价值

[英]Getting Value Outside Method

I have a ViewController that has a TableView and I want to Get the value from the didSelectRowAtIndexPath so i can pass to another view. 我有一个具有TableView的ViewController,我想从didSelectRowAtIndexPath获取值,以便可以传递到另一个视图。

This is the Method: 这是方法:

- (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;
}

I create and synthesize the property stringTest 我创建并合成属性stringTest

@property (nonatomic, strong) NSString *stringTest;
@synthesize stringTest;

to assign the value from selectRow but i guess that i'm doing something wrong. selectRow分配值,但我想我做错了。 Because this doesnt work. 因为这不起作用。

Anybody can help me? 有人可以帮助我吗? Thanks 谢谢

First, I would try to just see if you are assigning a value by nslog 首先,我会尝试看看您是否正在通过nslog分配值

NSLog(@"%@", [_vinhoArray objectAtIndex:indexPath.row]);

Then if you are getting something in the log, you can try 然后,如果您在日志中得到了一些东西,可以尝试

self.stringTest = [_vinhoArray objectAtIndex:indexPath.row];

if you are storing NSStrings in your array. 如果要在数组中存储NSStrings。

Try this, 尝试这个,

create your string in AppDelegate.h 在AppDelegate.h中创建您的字符串

@property (nonatomic, retain) NSString *myTitleString;

Now anywhere you want to access this string, you are going to need to include the appDelegate at the top 现在,要访问此字符串的任何地方,都需要在顶部包含appDelegate

#import "AppDelegate.h"

and then use this to access it 然后使用它来访问它

AppDelegate *AppDelegate = [[UIApplication sharedApplication] delegate];

assign it with 分配给

AppDelegate.MyTitleString = //whatever you are going to assign it with;

in your other view controller you will do the same thing, import the appdelegat.h at the top, create the app delegate and give it's value to your title 在其他视图控制器中,您将执行相同的操作,在顶部导入appdelegat.h,创建应用程序委托并将其值赋予标题

NSString *MyStringInTheDifferentController = AppDelegate.MyTitleString;

Insert the following method right after @synthesize stringTest; @synthesize stringTest;之后插入以下方法@synthesize stringTest; :

- (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. */
}

I think that this is the best way to debug because you may override the stringTest property right after tableView:didSelectRowAtIndexPath and you may not know it. 我认为这是调试的最佳方法,因为您可能会在tableView:didSelectRowAtIndexPath之后stringTest覆盖stringTest属性,而您可能不知道它。

PS: If you don't use ARC that setter needs to be updated. PS:如果您不使用ARC ,则需要更新设置器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM