目标VC无法找到prepareForSegue提供的ivars

时间:2013-03-14 08:06:06

标签: xcode

我有两个视图控制器:BSViewController,其中包含源ivars numberarray,以及BSotherViewController,它们作为目标需要接收ivars。 One way of producing the desired result was provided in this question。建议是使用prepareForSegue方法,我已经部分实现了,但是ivars并没有完全进入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

我有一个名为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

由此产生的控制台读数如下,并向我建议initWithNumber:array:的{​​{1}}部分正常工作。

prepareForSegue

但是目标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 ) 没有收到ivars。

BSotherViewController.h

BSotherViewController

BSotherViewController.m

#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

由此产生的控制台读数如下,并告诉我故事板“新引用插座”项可能有问题,但我无法连接该项,因为没有控制拖动“需要”。但这可能不是问题所在。

#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

1 个答案:

答案 0 :(得分:1)

prepareForSegue:sender:中,您创建了一个BSData对象,并且不执行任何操作。

- (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?
    }
}

您应该从segue获取目标控制器并为其属性赋值。您链接的答案显示了如何执行此操作。