• <tfoot id='y8okF'></tfoot>
      <bdo id='y8okF'></bdo><ul id='y8okF'></ul>

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

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

        使用委托将数据传回导航堆栈

        时间:2023-06-12
          <tbody id='Qw9T4'></tbody>
      2. <small id='Qw9T4'></small><noframes id='Qw9T4'>

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

                • <bdo id='Qw9T4'></bdo><ul id='Qw9T4'></ul>

                  <tfoot id='Qw9T4'></tfoot>
                  本文介绍了使用委托将数据传回导航堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  几天来,我一直在与在两个视图控制器之间传递数据作斗争,并且感到非常困惑.我是 Objective-C 的新手,发现有些地方很难搞清楚.

                  I have been battling with passing data between two view controllers for a couple of days now and getting very confused. I'm new to Objective-C and finding some parts tricky to get my head round.

                  我有一个导航控制器,FirstView 是一个表单,在这个表单上我有一个按钮,用于加载 SecondView,其中包含一个 TableView 供用户选择一些选项.然后我想将选择传递回 FirstView 控制器并显示数据等...

                  I have a Navigation Controller, FirstView is a form and on this form I have a button which loads SecondView which contains a TableView for the user to select some options. I then want to pass the selection back to the FirstView controller and display the data etc...

                  我已经阅读了很多关于此的内容(stackoverflow、iphonedevsdk、CS 193P 资源),我看到的选项是,

                  I have read alot about this (stackoverflow, iphonedevsdk, CS 193P Resources) and the options i've seen are,

                  1) 应用委托中的 ivar(脏,不推荐)2)创建一个单例3)创建数据模型类4) 使用协议和委托(苹果推荐)

                  1) ivar in app delegate (dirty and not recommended) 2) create a singleton 3) create a data model class 4) Use protocols and delegates (recommended by apple)

                  我想做正确的事并想在我的程序中使用选项 4 - 代表

                  I want to do things right and want to use option 4 - Delegates in my program

                  问题是,我不了解代表以及如何设置和实施它们.

                  Problem is, I don't understand delegates and how to setup and implement them.

                  谁能提供一个基本示例,说明如何使用委托和 2 个视图控制器设置和传递 NSArray.

                  Could anyone provide a basic example on how to setup and pass an NSArray using the delegate and 2 view controllers.

                  提前致谢马特

                  推荐答案

                  委托是在这种情况下使用的正确模式,但您的描述看起来不像委托,因为它使用全局变量.也许你在你的 App Delegate 中存储了全局变量,你应该尽量避免.

                  Delegation is the correct pattern to be used in this case, but your description doesn't look much like delegation as it is using a global variable. Perhaps you're storing global variables in your App Delegate which you should always try to avoid.

                  下面是代码的大致轮廓:

                  Here's a rough outline of what the code should look like:

                  SecondViewController.h:

                  @protocol SecondViewControllerDelegate;
                  
                  @interface SecondViewController;
                  
                  SecondViewController : UIViewController
                  {
                      id<SecondViewControllerDelegate> delegate;
                  
                      NSArray* someArray;
                  }
                  
                  @property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;
                  @property (nonatomic, retain) NSArray* someArray;
                  
                  @end
                  
                  @protocol SecondViewControllerDelegate
                  - (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController;
                  @end
                  

                  SecondViewController.m:

                  @implementation SecondViewController
                  
                  @synthesize delegate;
                  @synthesize someArray;
                  
                  - (void)dealloc
                  {
                      [someArray release];
                      [super dealloc];
                  }
                  
                  - (void)someMethodCalledWhenUserIsDone
                  {
                      [delegate secondViewControllerDidFinish:self];
                  }
                  

                  FirstViewController.h:

                  FirstViewController.h:

                  #import SecondViewController
                  
                  @interface FirstViewController : UIViewController <SecondViewControllerDelegate>
                  {
                      ...
                  }
                  
                  @end
                  

                  FirstViewController.m:

                  @implementation FirstViewController
                  
                  - (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController
                  {
                      NSArray* someArray = secondViewController.someArray
                      // Do something with the array
                  }
                  
                  @end
                  

                  这篇关于使用委托将数据传回导航堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:选择选项卡时弹出到根视图 下一篇:如何防止大标题折叠

                  相关文章

                  1. <small id='7SGOf'></small><noframes id='7SGOf'>

                  2. <tfoot id='7SGOf'></tfoot><legend id='7SGOf'><style id='7SGOf'><dir id='7SGOf'><q id='7SGOf'></q></dir></style></legend>

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

                        <bdo id='7SGOf'></bdo><ul id='7SGOf'></ul>