如何使用委托方法在两个视图控制器之间传递对象? [英] How to use delegate methods to pass objects between two view controllers?

查看:39
本文介绍了如何使用委托方法在两个视图控制器之间传递对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Xcode 4.2上使用Utility Application项目,然后为每个视图添加两个文本字段:主视图 flipside视图.现在,我想将 flipside 文本字段的值分配给mainview文本字段.

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. Now I wish to assign the value of the flipside text field to mainview text field.

MainViewController.h

      #import "FlipsideViewController.h"

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

      - (IBAction)showInfo:(id)sender;

      @end

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

      #import "FlipsideViewController.h"

      @implementation FlipsideViewController

      @synthesize delegate = _delegate;
      @synthesize changeText;

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

当完成操作开始时,我希望将 changetext 值分配给 nameField 文本.我该怎么做?

When the done action starts, i want the changetext value be assigned to nameField text. How do I do this?

推荐答案

MainViewController.m 中,将 flipSideViewControllerDelegate 方法实现为:

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

因此,当调用 done:方法时,也会使用您的 flipSideViewController 对象作为参数来调用此委托方法,通过它 changeText 可以访问.

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.

编辑以回答评论中的问题:

EDIT to answer question in comment:

在您的协议FlipSideViewControllerDelegate中,添加以下方法:

In your protocol FlipSideViewControllerDelegate, add this method:

 - (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath

然后,它类似于 MainViewController.m 中的其他委托方法实现.,这实际上就是该协议的工作方式.如果您的MainViewController符合该协议,则可以实现该协议的方法.默认情况下,协议中声明的所有方法都是可选的,但是您可以通过使用

Then it is similar to the other delegate method implementation in MainViewController.m , which is how the protocol works really. If your MainViewController conforms to the protocol, it can implement methods of that protocol. 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

请记住,如果您的方法在协议中声明为必需,则任何符合该协议的类都必须实现.无论如何,在您的 MainViewController.m 中:

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:

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

这篇关于如何使用委托方法在两个视图控制器之间传递对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆