0

In my project, there are two view controllers - let's say firstViewController and secondViewController. The second view controller has a button, and I want to make sure when the button gets pressed, the second view controller is telling somehow the first view controller - "hey, I got pressed, do something!", and it will do something, like changing a label. How is this possible to perform? Thanks in advance. Some code :

@interface firstViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation firstViewController

@synthesize textLabel;

@end


@interface secondViewController : UIViewController

-(IBAction)buttonPressed;

@end

@implementation secondViewController : UIViewController

-(IBAction)buttonPressed{
// Hey, I got pressed! Set the text on textLabel to "OK"!
}

@end

3 Answers 3

2

This is a very simple case of delegation and protocol mechanism of objective-c.. have a look at this tutorial which will explain you how its done.. you can do this via notification also but that is not usually advised...(because notification is usually used when the receiver is unknown , like in the case of UIDeviceBatteryLevelDidChangeNotification you don't exactly know which view controller wants to know about this.)

2

I'd first consider what the button press means. Does it change the state of the model?

Say your model is an int, and the button increments it. The view controllers wouldn't message each other about that, they would just both observe the state of the model. (The one with the button could change the state, too).

Thinking about it this way, the solution probably isn't delegation. It's probably notification or KVO.

0

See the answer to this question: Passing data between two view controllers via a protocol

However, ask yourself if you really need a protocol here. If it is just between this classes or just about the question of accessing data of a class or sending information to a class then that is what the interface of a class is made for.

@interface firstViewController : UIViewController{
    UILabel *textLabel;   // I personally alway add IBOutlet here too, but I think that is not required.
}

@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

And in SecondViewController.m:

#import "FirstViewController.h"

@implementation secondViewController : UIViewController

-(IBAction)buttonPressed{
  // You will have to have a properly set instance variable firstViewController
  [firstViewController.textLabel setText:@"OK"];
}

@end

So your second view controller needs to 'know' the first one. One way of achieving that is defining FirstViewController *firstViewController; as property and set it from wherever the second view controller is created and the first one is already known. How to do that exactly depends very much on the architecture of your app.

3
  • seems to me you are making a new object of FirstViewController so this will not work unless you create an object of FirstViewController in appdelegate.. Mar 17, 2012 at 16:39
  • @Hermann the approach you've suggested would require coupling the first view controller to the second. The purpose of patterns like delegation, notification, KVO, etc. is precisely to help us avoid that kind of coupling.
    – jlehr
    Mar 17, 2012 at 17:09
  • jlehr, that is right. But is it worth the effort if it is just about a single view controller class? Ankit, I did not say where the view controllers are actually creatd or hosted. Naturally there need to be a reference from one to the other. However using a protocol would not change that requirement in any way. Mar 17, 2012 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.