2

私はビューコントローラ1のテーブルから選択された行番号を2番目のビューコントローラに渡そうとする初心者です。2つのテーブルビューコントローラの間で番号オブジェクトを渡す:IOS

私はVC1での数のプロパティ宣言を使用してこれを実行しようとしています:

@property (nonatomic, retain) NSNumber *passedSectorNumber; 

次に、それをこのようにVC1に@synthesizedとVC1のdidSelectRowatIndexPathで適切な行番号が設定されます。

self.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]]; 
     VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil]; 
     [self.navigationController pushViewController:vc2 animated:YES]; 
     [vc2 release]; 

VC2では、同じ名前のNSNumberプロパティも定義し、それを整理します。 VC2において

も:

@property (nonatomic, retain) NSNumber *passedSectorNumber; 

IこうしてVC 2に渡された値をテスト:

NSInteger intvalue = [self.passedSectorNumber integerValue]; 
    NSLog(@"The value of the integer is: %i", intvalue); 

数は常に '0' であるVC2に "受信"、に関わらずどの行のあります選択された。

ルーキーエラーです。どんなアイデア?入力に非常に感謝します。

+2

'didSelectRowAtIndexPath'では、あなたが行う必要があります:' vc2.passedSectorNumber = [NSNumber numberWithInt:indexPath.row]; 'あなたがalloc-init' vc2を実行した後。そのためには、VC2で宣言するプロパティ 'passedSectorNumber'が必要です。 VC1でプロパティ 'passedSectorNumber'を宣言する必要はありません。 – albertamg

+0

あなたは 'vc2.passedSectorNumber = self.passedSectorNumber'を持っていますか? –

+0

>> albertamg、ありがとう、それはとてもうまくいきました。とても有難い。 –

答えて

0

2番目のVCがSectorEditorと呼ばれていると仮定すると:

VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil]; 
vc2.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]]; 
[self.navigationController pushViewController:vc2 animated:YES]; 
[vc2 release]; 

あるいはさらに良い、2番目のVCでクラスメソッドを宣言しinitWithPassedNumberと呼ばれ、その内部で呼び出します。

self.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]]; 
VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil]; 
[self.navigationController pushViewController:vc2 animated:YES]; 
[vc2 release]; 

は、このあるべき次のようなinitWithNibName:

- initWithPassedSectorNumber:(NSInteger)sectorNumber 
{ 
    if ((self = [super initWithNibName:@"vc2nibname" bundle:nil])) { 
     self.passedSectorNumber = sectorNumber 
    } 
} 

VC2 *vc2 = [[SectorEditor alloc] initWithPassedSectorNumber:indexPath.row bundle:nil]; 
[self.navigationController pushViewController:vc2 animated:YES]; 
[vc2 release]; 

コードはテストされていませんが、これは必要なものに近いでしょう。

関連する問題