0

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];
      }

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

MadhavanRP
  • 2,832
  • 1
  • 20
  • 26
Maurizio
  • 89
  • 2
  • 15

1 Answers1

2

In MainViewController.m, implement the flipSideViewControllerDelegate method as:

 - (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.

EDIT to answer question in comment:

In your protocol FlipSideViewControllerDelegate, add this method:

 - (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath

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

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];
}
MadhavanRP
  • 2,832
  • 1
  • 20
  • 26
  • Thk very much! If i would implement a table view in the flipsideViewController.m: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ..... } How pass the value of row in the text field main view? – Maurizio Dec 24 '11 at 11:04