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