View Full Version : Problems passing variables between viewcontrollers

Jan 13, 2009, 04:34 PM
I'm having some problems passing variables between viewcontrollers.., I've got two viewcontrollers, one pops up to adjust settings/parameters for searching, so it needs to pass some values to it's parent viewcontroller. But I can't seem to get the variable out of the local ViewController at all..

I've currently got a 'ZoekenVarDelegate' class setup to hold the delegation of the value (it's not the main app delegate, but that gave the same result, so to clean it up I decided to move it to it's own class):


#import <UIKit/UIKit.h>
#import "CategorieSelectorViewController.h"
#import "ZoekenViewController.h"

@class CategorieSelectorViewController;
@class ZoekenViewController;

@interface ZoekenVarDelegate : NSObject <UIApplicationDelegate> {

ZoekenViewController *zoekenViewController;
CategorieSelectorViewController *categorieSelectorViewController;

@property (nonatomic, assign) ZoekenViewController *zoekenViewController;
@property (nonatomic, assign) CategorieSelectorViewController *categorieSelectorViewController;



#import "ZoekenVarDelegate.h"
@implementation ZoekenVarDelegate
@synthesize zoekenViewController, categorieSelectorViewController;

- (id)init
if (self = [super init])
categorieSelectorViewController.delegateRef = self;
return self;


Now the first controller that pops up is the 'ZoekenViewController', this has a searchbox with standard values, there's an options button that will popup a 'CategorieSelectorViewController' with PresentModalViewController, in this screen I want to populate some variables that I pass back to the 'ZoekenViewController'

I added a NSString that needs to be populated from 'CategorieSelectorViewController' to the 'ZoekenViewController':

@interface ZoekenViewController : [..]
NSString *categoriezoekID;
@property (nonatomic, retain) NSString *categoriezoekID;

And in the CategorieSelectorViewController I try to populate it (without success) like:

(part of) CategorieSelectorViewController.h

#import <UIKit/UIKit.h>
@class ZoekenVarDelegate;
@interface CategorieSelectorViewController : UIViewController <UIPickerViewDelegate> {
NSString *categorieID;
ZoekenVarDelegate *delegateRef;

@property (nonatomic, retain) NSString *categorieID;
@property (nonatomic, assign) ZoekenVarDelegate *delegateRef;


(part of) CategorieSelectorViewController.m

#import "CategorieSelectorViewController.h"
#import "ZoekenViewController.h"
#import "ZoekenVarDelegate.h"

@synthesize delegateRef;

- (IBAction)selecteerCategorie:(id)sender {
if (self.geselecteerdValue == @"Blah") { self.categorieID = @"1";};
self.delegateRef.zoekenViewController.categoriezoekID = self.categorieID;
[self dismissModalViewControllerAnimated:YES];

This is the main part that should happen, a user picks a value from a UIPickerView that is populated through an array and give back a value if it matches, the self.categorieID is properly set (debugger shows), but upon calling self.delegateRef.zoekenViewController.categoriezoekID = self.categorieID; it does nothing, it doesn't seem to populate a variable whatsoever or throw errors..

What am I missing here ?

Jan 13, 2009, 10:55 PM
Did you alloc/init the zoekenViewController? If it's nil, then there's no property to be set...

That said, you might want to look into using NSNotificationCenter instead. That way when something changes, you can just post a notification, and the controllers that care about it can watch for those notifications...

Jan 15, 2009, 11:02 AM
Got it working in the end, I wasn't thinking clearly. The viewcontrollers were already alloc'd through the IB (added them there, I know, bad practice but it's how I initially designed the whole thing, first app is a learning curve anyways ;-) ), so I was allocating and setting viewcontrollers that weren't actually used at all.

Calling the actual viewcontroller in use, made it work at an instant :D