2012-02-21 82 views
1

我在我的应用程序中使用了两个viewcontrollers。我的FirstViewController中有一个按钮。当我选择按钮时,它将推动SecondViewController。我在SecondViewController中有一个tableView。如何将SecondViewController的tableView中的选定索引的indexpath值传递给FirstViewController,而不使用NSUserDefaults?提前致谢。将indexpath值传递给FirstViewController

回答

2

使用协议。您可以使用委托将值传递回父视图。

使用委托(@Protocol)来回调父视图控制器总是更好。

上一个简单的教程,可以发现here

0

使用NSNotifications传递值。

1

你总是可以从SecondViewController访问通过访问FirstViewController - [UIViewController中parentController]例如,如果FirstViewController有一个名为selectedRowInSecondController属性:

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

排在第二的ViewController在创建变量的财产。

@property(nonatomic,readwrite)NSUInteger indexValue; 

然后

@synthesize indexValue= _indexValue; 

然后创建secondVctr在firstviewcontroller的对象之后;

#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]; 

}