UINavigationControllers:如何将值传递给堆栈中的更高(父?)控制器?

发布于 2024-11-26 04:39:03 字数 263 浏览 1 评论 0原文

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲喜皆因你 2024-12-03 04:39:03

如果您想实现此行为,请将对前一个视图控制器的引用传递给 SendingDetailController。这样,详细视图控制器可以向堆栈上的前一个视图控制器发送消息。

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

// 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 属性。

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

并编写将接收字符串的方法。

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

这应该可以完成工作。

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.

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

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

This should do the job.

淡紫姑娘! 2024-12-03 04:39:03

为了在不同的 UIViewController 之间轻松进行异步通信,您可能需要查看 NSNotificationNSNotificationCenter

网上有很多教程,SO 上有一些很好的答案,可以向您展示如何准确地做到这一点。

For easy asynchronous communication between different UIViewControllers, you may want to look at NSNotification and NSNotificationCenter.

There are plenty tutorials on the web and some good answers here at SO that can show you how to do that exactly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文