Hello,
I know this question as been asked over and over but it's still quite obscure to me, so I guess making an example with my code instead will probably be easier .
I know that you can use :
- A global variable, ( not good practice ).
- Use a delegate
- Use a singleton
Say I've got this piece of code here in my first view controller header :
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
IBOutlet UITextField *Te;
NSInteger evTe;
}
@property (nonatomic, retain) UITextField *Te;
@property (nonatomic) NSInteger evTe;
- (IBAction) makeKeyboardGoAway;
@end
and then this in my implementation file
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize Te;
- (IBAction) makeKeyboardGoAway;
{
[Te resignFirstResponder];
evTe = [Te.text intValue];
}
How would I call evTe in my SecondViewController ? ( maybe using a delegate ?) .
This is what I've got in the second view Controller, header :
@interface SecondViewController : UIViewController {
NSInteger evTe;
}
@property (nonatomic) NSInteger evTe;
and implementation :
- (IBAction) makeKeyboardGoAway;
{
FirstViewController *first = [[FirstViewController alloc] init];
first.evTe = self.evTe;
NSLog(@"second value is %i",evTe);
}
Thanks a lot !
Edit for Tob
FirstViewController.m
- (IBAction) makeKeyboardGoAway;
{
evTe = [Te.text intValue];
NSLog(@"The value of integer num is %i", evTe);
NSDictionary *changedValues = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:evTe] forKey:@"evTe"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"evTeChanged" object:self userInfo:changedValues];
}
SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToCall:) name:@"evTeChanged" object:nil];
}
- (void)methodToCall:(NSNotification *)aNotification{
NSDictionary *changedValues = [[aNotification userInfo] objectForKey:@"evTe"];
NSString *dictionaryString = [changedValues description];
NSLog(@"Notification returning %d",dictionaryString);
}
Unfortunately I'm not getting any log from the SecondView ..