0

I have two view controllers: BSViewController which contains the source ivars number and array, and BSotherViewController which as the target needs to receive the ivars . (BSViewController has a button on it that is segues to BSotherViewController.)

How can I access the value of the two ivars in BSotherViewController?

BSViewController.h

#import <UIKit/UIKit.h>

@interface BSViewController : UIViewController
@property (nonatomic) NSInteger number;
@property (nonatomic, weak) NSArray * array;
@end

BSViewController.m

#import "BSViewController.h"

@interface BSViewController ()
@end
@implementation BSViewController
@synthesize number;
@synthesize array;

- (void)viewDidLoad
{
    [super viewDidLoad];
    BSViewController *view = [[BSViewController alloc] init];
    NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
    view.array = _array;
    view.number = 25;
}

@end

BSotherViewController.h

#import <UIKit/UIKit.h>

@class BSViewController;
@interface BSotherViewController : UIViewController
@end

BSotherViewController.m

The problem below is that aview.number is 0, not 25; and aview.array is null.

#import "BSotherViewController.h"
#include "BSViewController.h"
@interface BSotherViewController ()

@end

@implementation BSotherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    BSViewController *aview = [[BSViewController alloc] init];
    NSLog(@"other view: %@", aview);
    NSLog(@"other number: %d", aview.number);  // produces 0, not desired 25
    NSLog(@"other array: %@", aview.array);    // produces null, not desired manny,moe
}
@end
zerowords
  • 2,915
  • 4
  • 29
  • 44

2 Answers2

1

When you instantiate the BSViewController from BSOtherViewController you are calling the init method. The values is set in viewDidLoad in BSViewController, and that method won't be invoked until the view is actually loaded.

Try to override the init method and set the values

- (id)init {

if (self = [super init]) {
    //Set values
     NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
     self.array = _array;
     self.number = 25;
}
return self;
}
johan
  • 6,578
  • 6
  • 46
  • 68
  • How do I fix it? If I put `@property (strong, nonatomic) BSViewController *view;` into .h I get incompatible property type. – zerowords Mar 12 '13 at 11:57
  • Add the init method to BSViewController and set the values in that method – johan Mar 12 '13 at 11:58
  • That's cheating, isn't it, because the `init` is being executed before the values of number and array are known. I have tried to create another `- (id)initWithNumber:(NSInteger)number array:(NSArray *)array { self = [super init]; if (self) { _number = number; _array = array; return self; } return nil; }` and call it after `number` and `array` are computed, but I get 0 and null results. Can you please react to these ideas, too? – zerowords Mar 12 '13 at 19:22
  • Why do you have a weak reference to the array? And try to add (nonatomic, assign) to the NSInteger – johan Mar 13 '13 at 12:44
  • Folks, I have resisted accepting this answer because I have been finding other things get in the way of it all working. [For example](http://stackoverflow.com/questions/15372796/] continues the question. Also, most recently I have discovered that it makes a difference whether the segue involved is a push or a modal segue. When I embed the VCs involved in UINavigationControllers, only the push segue works, not the modals. I don't know why, but likely that has been an unseen problem in my approaches and questions. Thanks for everyone's help. – zerowords Mar 16 '13 at 21:38
  • You're wrong! That question is not at all the same as the one answered in this post. Here you explicitly call init of the view controller, hence this solution will work. You don't even mention a storyboard. – johan Mar 17 '13 at 12:39
0

Replace your BSViewController viewDidLoad method with init like this :

- (id)init {

if (self = [super init]) {

    NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
    self.array = _array;
    view.number = 25;

}
return self;
}
arun.s
  • 1,528
  • 9
  • 12
  • Nice answer. Looks alot like mine :) I think view.array should be replaced with self.array. – johan Mar 12 '13 at 12:55
  • oops .... copy pasted from zerowords code ... didn't noticed it .. correcting now ... tnx :) @Johan : The init will be like this, whoever answers this questions .... so may be its looking similar .. ;) – arun.s Mar 12 '13 at 13:40
  • Please see my comment to the other answer (about cheating?). – zerowords Mar 12 '13 at 19:28