2012-02-21 6 views
1

私のアプリケーションで2つのビューコントローラーを使用しています。 FirstViewControllerにボタンがあります。ボタンを選択すると、SecondViewControllerがプッシュされます。私はSecondViewControllerのtableViewを持っています。 NSUserDefaultsを使用せずにSecondViewControllerのtableViewで選択したインデックスのindexpath値をFirstViewControllerに渡す方法前もって感謝します。indexpathの値をFirstViewControllerに渡します。

答えて

2

プロトコルを使用します。代理人を使用して、親ビューに値を戻すことができます。

親ビューコントローラをコールバックするには、デリゲート(@Protocol)を使用する方が常に優れています。その上

簡単なチュートリアルでは、値を渡すhere

0

使用NSNotificationsを見つけることができます。

1

あなたは常にSecondViewControllerアクセス​​からFirstViewControllerを介してアクセスすることができます - FirstViewControllerがselectedRowInSecondControllerという名前のプロパティがある場合、[のUIViewController parentController]例えば:それは変数のプロパティを作成する際に第二のViewControllerで

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
FirstViewController *firstViewController = [self parentController]; 
firstViewController.selectedRowInSecondController = [indexPath row]; 
} 
1

を。

@property(nonatomic,readwrite)NSUInteger indexValue; 

次いで

@synthesize indexValue= _indexValue; 

この後firstviewcontrollerにsecondVctrのオブジェクトを作成します。

#import "secondViewcontroller.h" 

    @interface firstviewcontroller : UIViewController 

@property(nonatomic,strong)secondViewcontroller *objsecondViewcontroller; 


@synthesize objsecondViewcontroller =_objsecondViewcontroller; 

次のコードは、ユーザーがそのイベントのボタンをタップしたときに挿入されます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  

    CountDown *controller = [[secondvctr alloc] initWithNibName:@"secondvctr" bundle:nil]; 
    self.objsecondViewcontroller.indexValue=indexPath 
    [self.navigationController pushViewController:controller animated:YES]; 

} 
関連する問題