UINavigationControllers:如何将值传递给堆栈中更高的(父级?)控制器? [英] UINavigationControllers: How to pass value to higher (parent?) controller in stack?

查看:27
本文介绍了UINavigationControllers:如何将值传递给堆栈中更高的(父级?)控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 SendingController,它推送到导航堆栈 SendingDetailsController(其中有一个 TableView).用户应该在 TableView 中选择一行(它通过 Checkmark 检查),我想将此行的值(让它成为 NSString 对象)传递给 SendingController.

I have SendingController which push to nav stack SendingDeatilsController (which one has a TableView). User should should pick in TableView one row (it checked by Checkmark) and I would like to pass the value of this row (let it will NSString object) to the SendingController.

如何在我的应用程序中实现这种行为?SendingController 是 SendingDetailController 的父级吗(SDC 的属性 parentController 是指 SC)??

How can I realize this behaviour in my application? And is SendingController parent for SendingDetailController (attribute parentController of SDC refers to SC) ??

推荐答案

如果要实现此行为,请将 SendingDetailController 传递给前一个视图控制器的引用.这样,细节视图控制器就可以向堆栈中的前一个控制器发送消息.

If you want to implement this behaviour, pass the SendingDetailController a reference to the previous view controller. This way the detail view controller can send a message to the previous one on the stack.

在你的 SendingDetailController 中定义一个弱引用:

In your SendingDetailController define a weak reference :

// in .h
SendingController *sendingController;
@property(assign) SendingController *sendingController;

// in .m
@synthesize sendingController;

-(void)tableView:(UITableView *)tableView
 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // retrieve the string and send the message
    [sendingController didSelectString:theString];
}

现在在将 SendingDetailController 推送到堆栈之前不要忘记设置它的 sendingController 属性.

Now before pushing the SendingDetailController on the stack don't forget to set its sendingController property.

// .m
// where you push the vc
if(!sendingDetailController) {
    sendingDetailController = [[SendingDetailController alloc]
                               initWithNibName:@"TheNIBName"
                                        bundle:nil];
    sendingDetailController.sendingController = self;
}
[self.navigationController pushViewController:sendingDetailController
                                     animated:YES];

并编写接收字符串的方法.

and write the method that will recieve the string.

-(void)didSelectString:(NSString *)aString {
    // do anything with string
    [self.navigationController popViewControllerAnimated:YES];
}

这应该可以完成工作.

这篇关于UINavigationControllers:如何将值传递给堆栈中更高的(父级?)控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆