简体   繁体   中英

Passing variables from one view to an other

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM