将变量传递给父视图?

发布于 2025-01-08 22:07:39 字数 162 浏览 3 评论 0原文

我有一个 UITableViewController ,它推送一个 UIViewController ,我需要将两个 NSDate 和一个 BOOL 从该子视图控制器传递回表视图控制器。

我怎么能这样做呢?如果我不清楚或者您需要更多解释,请告诉我。

我将不胜感激任何帮助,谢谢。

I have a UITableViewController which pushes a UIViewController and I need to pass two NSDates and a BOOL from that child view controller back to the table view controller.

How could I do this? Let me know if I am not being clear or you need more explanation.

I'd appreciate any help, thanks.

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

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

发布评论

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

评论(1

温柔一刀 2025-01-15 22:07:39

使用委托是将信息从一个控制器传递到另一个控制器的正确方法。

一般来说,您要做的就是在 UITableViewController 标头中声明一个协议,实现它,然后使用委托初始化 UIViewController

简而言之,您的 UITableViewController .h 文件应该包含如下内容:

@protocol setDateDelegate <NSObject>
@required
- (void) setDateOne:(NSDate *)one dateTwo:(NSDate *)two;
@end

然后,您应该实现上述类,对 UITableViewController .m< 中的日期执行您想要执行的任何操作/代码> 文件。

在你的 UIViewController 中,你可以(最后选择)定义一个自定义 init 方法:

首先,在 .h 中定义一个委托属性和一个自定义 init 方法,然后在 .m 中使用它来实现它像这样的事情:

- (id)initWithDelegate:(NSObject *)myDelegate
{

    self = [super init];
    if (self) {
        self.delegate = myDelegate;
    }
    return self;
}

接下来,当您需要将日期传递给 UITableViewController 时,只需使用
[self.delegate setDateOne:one dateTwo:two];

还有更多方法可以做到这一点,包括跳过自定义 init 方法并在创建 UIViewController 后设置委托:

MyUIViewController *viewController = [[[MyUIViewController alloc] init] autorelease];
viewController.delegate = self;
...

这更多的是个人喜好风格。

我可能搞乱了代码(并且忘记了一些位),因为我是凭空写下这段代码的,但它应该足以让您开始使用。

Using a delegate is the proper way of passing info from one controller to another.

Generally, what you want to do is to declare a protocol in your UITableViewController header, implement it, and then init the UIViewController with a delegate.

In a nutshell, your UITableViewController .h file should have something like this:

@protocol setDateDelegate <NSObject>
@required
- (void) setDateOne:(NSDate *)one dateTwo:(NSDate *)two;
@end

Then, you ought to implement the above mentioned class doing whatever you want to do with the dates in your UITableViewController .m file.

In your UIViewController, you could (alternative at the end) define a custom init method:

First, define a delegate property in your .h, and a custom init method, then implement it in your .m with something like this:

- (id)initWithDelegate:(NSObject *)myDelegate
{

    self = [super init];
    if (self) {
        self.delegate = myDelegate;
    }
    return self;
}

Next, when you need to pass the dates to your UITableViewController, just use
[self.delegate setDateOne:one dateTwo:two];

There are more ways of doing this, including skipping the custom init method and just setting the delegate after you create a UIViewController:

MyUIViewController *viewController = [[[MyUIViewController alloc] init] autorelease];
viewController.delegate = self;
...

It's more of a personal preference style.

I've probably messed up the code (and forgotten a few bits) because I'm writing this off the top of my head, but it should be good enough to get you started.

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