将indexpath值传递给FirstViewController

时间:2012-02-21 10:39:41

标签: iphone ios uitableview uiviewcontroller

我在我的应用程序中使用了两个viewcontrollers。我的FirstViewController中有一个按钮。当我选择按钮时,它会推送SecondViewController。我在SecondViewController中有一个tableView。如何在不使用NSUserDefaults的情况下将SecondViewController的tableView中所选索引的索引路径值传递给FirstViewController?提前谢谢。

4 个答案:

答案 0 :(得分:2)

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

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

可以找到关于它的简单教程here

答案 1 :(得分:1)

例如,如果FirstViewController具有名为selectedRowInSecondController的属性,您始终可以从SecondViewController访问FirstViewController访问 - [UIViewController parentController]:

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

答案 2 :(得分:1)

在第二个viewcontroller中创建变量的属性。

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

}

答案 3 :(得分:0)

使用NSNotifications传递值。

相关问题