我有两个视图控制器:BSViewController
其中包含源 ivarsnumber
和array
,以及BSotherViewController
作为目标需要接收 ivars 。 这个问题提供了一种产生预期结果的方法。建议是使用 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:
部分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
)
但目标BSotherViewController
没有收到 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
由此产生的控制台读数如下,并向我表明情节提要“新参考插座”项目可能有问题,但我无法连接该项目,因为没有控制拖动“需要”。但这可能根本不是问题。
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)