<i id='9lyKC'><tr id='9lyKC'><dt id='9lyKC'><q id='9lyKC'><span id='9lyKC'><b id='9lyKC'><form id='9lyKC'><ins id='9lyKC'></ins><ul id='9lyKC'></ul><sub id='9lyKC'></sub></form><legend id='9lyKC'></legend><bdo id='9lyKC'><pre id='9lyKC'><center id='9lyKC'></center></pre></bdo></b><th id='9lyKC'></th></span></q></dt></tr></i><div id='9lyKC'><tfoot id='9lyKC'></tfoot><dl id='9lyKC'><fieldset id='9lyKC'></fieldset></dl></div>

    <tfoot id='9lyKC'></tfoot>

        <bdo id='9lyKC'></bdo><ul id='9lyKC'></ul>

    1. <small id='9lyKC'></small><noframes id='9lyKC'>

    2. <legend id='9lyKC'><style id='9lyKC'><dir id='9lyKC'><q id='9lyKC'></q></dir></style></legend>
    3. 使用委托和协议在 2 个 UIViewController 之间传递数据

      时间:2023-07-28

        <legend id='bBWhW'><style id='bBWhW'><dir id='bBWhW'><q id='bBWhW'></q></dir></style></legend>
          <bdo id='bBWhW'></bdo><ul id='bBWhW'></ul>

            <small id='bBWhW'></small><noframes id='bBWhW'>

            <tfoot id='bBWhW'></tfoot>

          • <i id='bBWhW'><tr id='bBWhW'><dt id='bBWhW'><q id='bBWhW'><span id='bBWhW'><b id='bBWhW'><form id='bBWhW'><ins id='bBWhW'></ins><ul id='bBWhW'></ul><sub id='bBWhW'></sub></form><legend id='bBWhW'></legend><bdo id='bBWhW'><pre id='bBWhW'><center id='bBWhW'></center></pre></bdo></b><th id='bBWhW'></th></span></q></dt></tr></i><div id='bBWhW'><tfoot id='bBWhW'></tfoot><dl id='bBWhW'><fieldset id='bBWhW'></fieldset></dl></div>
                  <tbody id='bBWhW'></tbody>
                本文介绍了使用委托和协议在 2 个 UIViewController 之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我知道这个问题已经在这里被问过很多次了.但我是第一次处理这件事,但仍然无法在我的脑海中完美地实现它.这是我实现的委托方法的代码,用于将数据从 SecondViewController 传递到 FirstViewController.

                I know this question has been asked and answered many times over here. But I am dealing with this thing for the first time and still not able to get the perfect implementation of it in my mind. Here's the code I have the delegate method I implement to pass data from SecondViewController to FirstViewController.

                FirstViewController.h

                #import "SecondViewController.h"
                
                @interface FirstViewController : UITableViewController<sampleDelegate> 
                @end
                

                FirstViewController.m

                @interface FirstViewController ()
                
                // Array in which I want to store the data I get back from SecondViewController.
                @property (nonatomic, copy) NSArray *sampleData;
                @end
                
                @implementation FirstViewController
                - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
                {
                   SecondViewController *controller = [[SecondViewController alloc] init];          
                   [self.navigationController pushViewController:controller animated:YES];
                }
                @end
                

                SecondViewController.h

                @protocol sampleDelegate <NSObject>
                - (NSArray*)sendDataBackToFirstController;
                @end
                
                @interface SecondViewController : UITableViewController
                @property (nonatomic, strong) id <sampleDelegate> sampleDelegateObject;
                @end
                

                SecondViewController.m

                @interface SecondViewController ()
                @property (strong, nonatomic) NSArray *dataInSecondViewController;
                @end
                
                @implementation SecondViewController
                
                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    self.dataInSecondViewController = [NSArray arrayWithObjects:@"Object1", @"Object2", nil];
                }
                
                - (NSArray*)sendDataBackToFirstController
                {
                    return self.dataInSecondViewController;
                }
                @end
                

                我做得对吗?我只希望它将 self.dataInSecondViewController 中的数据发送到 FirstViewController 并将其存储在 NSArray 属性 sampleDataFirstViewController 的代码>.

                Am I doing it correctly? All I want it to send the data in self.dataInSecondViewController to FirstViewController and store it over there in the NSArray property sampleData of FirstViewController.

                不知何故,我无法访问 FirstViewController 中的 sendDataBackToFirstController.我还缺少什么其他东西来访问 sendDataBackToFirstController 那里?

                Somehow I am not able to access sendDataBackToFirstController in FirstViewController. What other things I am missing implementing to access sendDataBackToFirstController there?

                推荐答案

                不太对.首先,您需要在第一个视图控制器中分配委托属性,以便第二个视图控制器知道要向哪个对象发送消息.

                Not quite right. First you need to assign the delegate property in the first view controller so the second view controller knows which object to send messages to.

                FirstViewController.m

                FirstViewController.m

                controller.delegate = self;
                

                其次,您可以向后发送和接收委托方法.您以 FirstViewController 预计在第二个控制器上调用 sendDataBackToFirstController 的方式设置它.在委托模式中,SecondViewController 是发送消息并可选择使用该方法发送数据的那个.所以,你应该把你的委托声明改成这样:

                Second, you have the sending and receiving of your delegate method backwards. You have it setup in a way where the FirstViewController is expected to call sendDataBackToFirstController on the second controller. In a delegate pattern, the SecondViewController is the one that sends the message and optionally sends data with that method. So, you should change your delegate declaration to something like this:

                @protocol sampleDelegate <NSObject>
                - (void)secondControllerFinishedWithItems:(NSArray* )newData;
                @end
                

                然后,当您的 SecondViewController 完成其任务并需要通知其委托时,它应该执行以下操作:

                Then, when your SecondViewController finishes its tasks and needs to notify its delegate, it should do something like this:

                // ... do a bunch of tasks ...
                // notify delegate
                if ([self.delegate respondsToSelector:@selector(secondControllerFinishedWithItems:)]) {
                    [self.delegate secondControllerFinishedWithItems:arrayOfNewData];
                }
                

                我在这里添加了一个额外的 if 语句来检查以确保委托会在实际发送之前响应我们想要发送的方法.如果我们的协议中有可选方法但没有这个,应用就会崩溃.

                I added an extra if statement here to check to make sure the delegate will respond to the method we want to send it before actually sending it. If we had optional methods in our protocol and did not have this, the app would crash.

                希望这会有所帮助!

                这篇关于使用委托和协议在 2 个 UIViewController 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:用 Swift 实现 UITextFieldDelegate 下一篇:在 ARC 下将代表设置为零?

                相关文章

                1. <legend id='pP5X8'><style id='pP5X8'><dir id='pP5X8'><q id='pP5X8'></q></dir></style></legend>
                    • <bdo id='pP5X8'></bdo><ul id='pP5X8'></ul>
                  1. <tfoot id='pP5X8'></tfoot>
                  2. <i id='pP5X8'><tr id='pP5X8'><dt id='pP5X8'><q id='pP5X8'><span id='pP5X8'><b id='pP5X8'><form id='pP5X8'><ins id='pP5X8'></ins><ul id='pP5X8'></ul><sub id='pP5X8'></sub></form><legend id='pP5X8'></legend><bdo id='pP5X8'><pre id='pP5X8'><center id='pP5X8'></center></pre></bdo></b><th id='pP5X8'></th></span></q></dt></tr></i><div id='pP5X8'><tfoot id='pP5X8'></tfoot><dl id='pP5X8'><fieldset id='pP5X8'></fieldset></dl></div>

                    <small id='pP5X8'></small><noframes id='pP5X8'>