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 . One way of producing the desired result was provided in this question. The suggestion was to use a prepareForSegue method and I have partially implemented that, but the ivars are not quite getting to the BSotherViewController.

BSViewController.m

#import "BSViewController.h"
#import "BSData.h"
@interface BSViewController ()
@end
@implementation BSViewController
@synthesize number;
@synthesize array;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"identifier=%@ destination=%@", segue.identifier, segue.destinationViewController);
    if ([segue.identifier isEqualToString:@"goToOtherVC"]) {
        BSData *data;
        data = [[BSData alloc] initWithNumber:self.number array:self.array];
    }
}

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

I have a model class named BSData.

BSData.h

#import <Foundation/Foundation.h>
@interface BSData : NSObject
@property (nonatomic) NSInteger number;
@property (nonatomic, copy) NSArray * array;
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array;
@end

BSData.m

#import "BSData.h"

@implementation BSData
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
    self = [super init];
    if (self) {
        _number = number;
        _array = array;

        NSLog(@"data number: %d", number);
        NSLog(@"data array: %@", array);
        return self;
    }
    return nil;
}
@end

And the resulting console readout is as follows and suggests to me the initWithNumber:array: part of prepareForSegue is working correctly.

2013-03-14 03:06:28.023 tester[42514:11303] data number: 25
2013-03-14 03:06:28.023 tester[42514:11303] data array: (
    manny,
    moe
)

But the target BSotherViewController is not receiving the ivars.

BSotherViewController.h

#import <UIKit/UIKit.h>
@class BSData;
@interface BSotherViewController : UIViewController <UINavigationControllerDelegate>
@property (nonatomic) NSInteger number;
@property (nonatomic, weak) NSArray * array;
@property (strong, nonatomic) BSData *data;
@end

BSotherViewController.m

#import "BSData.h"
#import "BSotherViewController.h"
@interface BSotherViewController ()
- (void)configureView;
@end

@implementation BSotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"other view: %@", self.data);
    NSLog(@"other number: %d", self.number);
    NSLog(@"other array: %@", self.array);
}
@end

And the resulting console readout is as follows and suggests to me that there may be something wrong with a storyboard "New Referencing Outlet" item, but I have not been able to connect that item because no control-drag "takes". But that may not be the problem at all.

2013-03-14 03:51:54.423 tester[43130:11303] other view: (null)
2013-03-14 03:51:54.424 tester[43130:11303] other number: 0
2013-03-14 03:51:54.425 tester[43130:11303] other array: (null)
Community
  • 1
  • 1
zerowords
  • 2,915
  • 4
  • 29
  • 44

1 Answers1

1

In your prepareForSegue:sender: you create a BSData object and do nothing with it.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"identifier=%@ destination=%@", segue.identifier, segue.destinationViewController);
    if ([segue.identifier isEqualToString:@"goToOtherVC"]) {
        // local variable is declared
        BSData *data;
        // local variable is initialized with newly created BSData object
        data = [[BSData alloc] initWithNumber:self.number array:self.array];
        // now what?
    }
}

You should obtain destination controller from segue and assign values to its properties. The answer you linked shows how to do this.

Kreiri
  • 7,840
  • 5
  • 30
  • 36
  • If you mean adding where you say "now what?" `data.number = self.number;` `data.array = self.array;`, that does not do anything that has not already been done because at that point both `data.number` and `data.array` already have the correct values. I don't understand how to " obtain destination controller from segue and assign values to its properties." I don't see the answer in the answer I linked. Please say more. – zerowords Mar 14 '13 at 08:46
  • 1
    Okay. `segue.destinationViewController`. Still can't see it there? – Kreiri Mar 14 '13 at 09:02