views:

71

answers:

2

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

A: 

Have a look at NSNotification. You should send a notification that the particular value has changed and register for that notification in the second view controller.

- (IBAction) makeKeyboardGoAway;
{
    [Te resignFirstResponder];  
    evTe = [Te.text intValue];

    NSDictionary *changedValues = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:evTe] forKey:@"evTe"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"evTeChanged" object:self userInfo:changedValues];

}

And in the viewDidLoad method of the other controller do:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToCall:) name:@"evTeChanged" object:nil];

Now every time the first controller calls makeKeyboardGoAway the method - (void)methodToCall:(NSNotification *)aNotification will be called.

Implement this method and ask the aNotification for its userInfo which is the NSDictionary you created in the first controller before posting the notification. Get the evTe value out of it and do whatever you want to do with that value.

tob
I'm sure this is also a correct way to go, but if you had 1 mn to give me an example that would be helpful, I find the documentation quite cryptic on this subject ..
Julz
I've edited my answer. Hope that helps.
tob
Hey Tob, thanks for your help.I've implemented ( in my secondViewController.m ):- (void)methodToCall:(NSNotification *)aNotification{ NSDictionary *changedValues = [[aNotification userInfo] objectForKey:@"evTe"]; NSString *dictionaryString = [changedValues description]; NSLog(@"Notification returning %d",dictionaryString); }But I don't get any log in my terminal, which leads me to think that the methodToCall function is not being executed .Do you see anything obviously wrong in the way I`m implementing this function ?
Julz
Hmm, bad formatting, let me edit the question instead ..
Julz
Did you register for the notification in viewDidLoad?
tob
No, I didn`t I guess I should read the doc more when it comes to NSNotifications ..
Julz
And does it work if you do?
tob
Your talking about the ViewDidLoad of the SecondViewController, right ? if so, yes I`ve got :[super viewDidLoad];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToCall:) name:@"evTeChanged" object:nil];
Julz
And did you initialize the controller before makeKeyboardGoAway is called in the first controller?
tob
A: 

By creating a @property called evTe or whatever in both view controllers.

If the FirstViewController is responsible for creating the SecondViewController you could store the value of evTe in a property in FirstViewController and then after you have created the SecondViewController you set the evTe property there as well.

- (IBAction) makeKeyboardGoAway;
{
    [Te resignFirstResponder];  
    self.evTe = [Te.text integerValue];
}

// other method where SecondViewController is created

SecondViewController* second = [[SecondViewController alloc] init];
second.evTe = self.evTe;
// do what ever

--Edit--

@interface FirstViewController : UIViewController {

    IBOutlet UITextField *Te;
    NSInteger evTe;

}

@property (nonatomic, retain) UITextField *Te;
@property (nonatomic) NSInteger evTe;
willcodejavaforfood
Hey thanks for the help, I`m not quite there yet, I`m getting this warning : Passing argument 1 of 'setevTe:' makes pointer from integer without a cast .Then it crashes :) But the rest looks sane .Thanks !
Julz
How did you declare the evTe property? :)
willcodejavaforfood
probably like a noob ? :@property (nonatomic, retain) UITextField *evTe;
Julz
Hold on, I thought evTe was an int property? :)
willcodejavaforfood
yes, of course, sorry was distracted, I`ve switched it to :@property int *evTe;But I`m still getting the value at 0, could it be because I`m instanciating a new variable therefore defaulting to 0 instead of returning the value in FirstViewController ?
Julz
Is it still crashing?
willcodejavaforfood
We need to crack this. I've updated my example to how it should look, you need the evTe property in both controllers. I've changed from intValue to integerValue, mostly because I am not sure how to deal with ints in objective-c :(
willcodejavaforfood
It's not crashing anymore, but the value I`m getting from the secondViewController is still zero unfortunately .Here is how it looks now, I`ve edited the original post, no issues compiling, no warning, if I print the evTe value from the firstViewController I get something meaningful but still 0 from the secondViewController . Isn`t it because we are instantiating a new variable therefore it's set to 0 by default ?
Julz
Are you creating SecondViewController before makeKeyboardGoAway is called?
willcodejavaforfood