2014-01-20 30 views
0

我有一個ViewController,它有一個TableView,我想從didSelectRowAtIndexPath中獲取值,所以我可以傳遞給另一個視圖。在方法外部獲取值

這是方法:

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

的值從selectRow轉讓,但我想我做錯了什麼。因爲這不起作用。

有人可以幫助我嗎?由於

+0

那麼它是'stringTest'還是'stringTeste'? – sha

+0

@sha ** stringTest **!對不起,輸入錯誤。 –

回答

1

首先,我會嘗試只是看看你是通過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,創建應用程序的委託,並給它的價值,你的標題

NSString *MyStringInTheDifferentController = AppDelegate.MyTitleString; 
+0

我在這裏嘗試和YES,我給'stringTest'分配一個值,但是隻在方法內部工作,如果我把NSLog放在方法外面,我會得到一個'Null'值。 我想在方法外使用,所以我可以傳遞給另一個視圖。 –

+0

'self.stringTest = [_vinhoArray objectAtIndex:indexPath。行];'應該在方法內工作。當然,NSLog語句不能在該方法之外工作,因爲它使用了該方法提供的indexPath。你有沒有嘗試給它分配'self.stringTest'? –

+0

是的,如果把'self.stringTest'放到了我的值。 但只在方法內部。我可以在這個方法之外使用嗎? 所以我可以在另一個ViewController中使用它? –

0

插入下面的方法@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. */ 
} 

我認爲這是調試,因爲你的最佳途徑可能會覆蓋tableView:didSelectRowAtIndexPath之後的stringTest產權,您可能不知道。

P.S .:如果您不使用ARC該setter需要更新。

+0

如果把這個方法一個得到一個錯誤 '使用未聲明的標識符_stringTest;你的意思是'StringTest'?' 我使用ARC,我正在使用XCode 5 ios7 –

+0

然後請從'@synthesize stringTest;'更新爲'@synthesize stringTest = _stringTest'; – arturgrigor

+0

好吧,現在是對的。 'stringTest'獲得正確的值。 但是我如何在另一個ViewController中使用這個值?我如何傳遞這個值並在另一個ViewController中使用? –