简体   繁体   English

如何使用委托方法在两个视图控制器之间传递对象?

[英]How to use delegate methods to pass objects between two view controllers?

I'm using the Utility Application project to Xcode 4.2, then I am adding two text fields for each view: main view and the flipside view . 我正在使用Xcode 4.2的Utility Application项目,然后我为每个视图添加两个文本字段: main viewflipside view Now I wish to assign the value of the flipside text field to mainview text field. 现在我希望将flipside文本字段的值分配给主视图文本字段。

MainViewController.h MainViewController.h

      #import "FlipsideViewController.h"

      @interface MainViewController : UIViewController <FlipsideViewControllerDelegate>  {
        UITextField *nameField;
      }
      @property(nonatomic, retain) IBOutlet UITextField *nameField;

      - (IBAction)showInfo:(id)sender;

      @end

FlipsideViewController.h FlipsideViewController.h

      @class FlipsideViewController;

      @protocol FlipsideViewControllerDelegate
         - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
      @end

      @interface FlipsideViewController : UIViewController {
          UITextField *changeText;
      }
      @property (nonatomic, retain) IBOutlet UITextField *changeText;
      @property (assign, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;

      - (IBAction)done:(id)sender;

      @end

FlipsideViewController.m FlipsideViewController.m

      #import "FlipsideViewController.h"

      @implementation FlipsideViewController

      @synthesize delegate = _delegate;
      @synthesize changeText;

      - (IBAction)done:(id)sender
      { 
        [self.delegate flipsideViewControllerDidFinish:self];
      }

When the done action starts, i want the changetext value be assigned to nameField text. 完成操作开始后,我希望将changetext值分配给nameField文本。 How do I do this? 我该怎么做呢?

In MainViewController.m , implement the flipSideViewControllerDelegate method as: MainViewController.m中 ,将flipSideViewControllerDelegate方法实现为:

 - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
self.nameField.text=controller.changeText.text;
}

So when the done: method is called, this delegate method is also called with your flipSideViewController object as the argument, through which changeText can be accessed. 因此,当调用done:方法时,也会使用flipSideViewController对象作为参数调用此委托方法,通过该changeText可以访问changeText

EDIT to answer question in comment: 编辑回答评论中的问题:

In your protocol FlipSideViewControllerDelegate, add this method: 在您的协议FlipSideViewControllerDelegate中,添加以下方法:

 - (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath

Then it is similar to the other delegate method implementation in MainViewController.m , which is how the protocol works really. 然后它类似于MainViewController.m中的其他委托方法实现,这是协议的真正工作方式。 If your MainViewController conforms to the protocol, it can implement methods of that protocol. 如果您的MainViewController符合协议,它可以实现该协议的方法。 By default all the methods declared in the protocol are optional, but you have the option to specify if the method is optional or required by using 默认情况下,协议中声明的所有方法都是可选的,但您可以选择指定方法是可选的还是必需的

@optional
//list of methods
@required
//list of methods

Bear in mind that if your a method is declared as required in the protocol, any class conforming to that protocol must implement it. 请记住,如果您的方法在协议中声明为必需,则符合该协议的任何类都必须实现它。 Anyway, in your MainViewController.m : 无论如何,在你的MainViewController.m中

- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
{
int anInt=indexPath.row;
self.nameField.text=[NSString stringWithFormat:@"%d",anInt];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM