从detailViewController获取bool值的最佳方法是什么?

发布于 2024-09-26 16:35:27 字数 133 浏览 1 评论 0原文

在我的基于导航的应用程序中,有一个按钮,如果按下该按钮,会将视图更改为detailViewController。在这里用户可以设置几个选项。这些选项之一是布尔值。当我从detailViewController返回时,我怎样才能看到这个bool值是什么?

In my navigation based app, there is a button that if pressed, will change the view to a detailViewController. Here the user can set several options. One of those options is a bool value. When I return from the detailViewController how can I see what this bool value is?

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

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

发布评论

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

评论(1

无人接听 2024-10-03 16:35:27

创建 协议DetailViewDelegate?)并创建一个delegate DetailViewController 中的属性。当您实例化视图控制器时,将委托属性设置为 self 并使用该属性将消息发送回主视图控制器。唯一棘手的部分是您需要将委托属性声明为“分配”,这样您就不会在详细视图和主视图之间创建保留循环。


DetailViewController.h:

@class DetailViewController; // Forward Declaration.
@protocol DetailViewDelegate
- (void)detailViewController:(DetailViewController *)controller didChangeBool:(BOOL)theBool;
@end

@interface DetailViewController : UIViewController {
    id <DetailViewDelegate> delegate;
}

@property (assign) id <DetailViewDelegate> delegate;

@end

这只是界面,但它应该可以帮助您完成大部分工作。设置详细视图的 delegate 属性,并在主视图中实现 detailViewController:didChangeBool: 方法,仅此而已。


回答您评论中的问题:

  1. 是的。在推送详细视图控制器之前,将其 delegate 属性设置为 self
  2. 您需要声明您的主视图控制器实现 DetailViewDelegate 协议。通过阅读 Apple 文档了解如何执行此操作。
  3. 在声明主视图控制器将实现该协议后,您需要实际实现它。将 detailViewController:didChangeBool: 方法添加到主视图控制器中。

Create a protocol (DetailViewDelegate?) and create a delegate property in your DetailViewController. When you instantiate your view controller, set the delegate property to self and use that property to send messages back to your master view controller. The only tricky part is that you need to declare the delegate property as "assign" so that you don't create a retain loop between your detail view and master view.


DetailViewController.h:

@class DetailViewController; // Forward Declaration.
@protocol DetailViewDelegate
- (void)detailViewController:(DetailViewController *)controller didChangeBool:(BOOL)theBool;
@end

@interface DetailViewController : UIViewController {
    id <DetailViewDelegate> delegate;
}

@property (assign) id <DetailViewDelegate> delegate;

@end

That's just the interface, but it should get you most of the way there. Set the delegate property of the detail view and implement a detailViewController:didChangeBool: method in your master view and that's about it.


To answer the questions in your comment:

  1. Yes. Before you push the detail view controller set it's delegate property to self.
  2. You need to declare that your master view controller implements the DetailViewDelegate protocol. Learn how to do that by reading Apple's Documentation.
  3. After you declare that your master view controller will implement the protocol, you need to actually implement it. Add a detailViewController:didChangeBool: method to your master view controller.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文