0

2つのViewControllerがあります。1つBSViewControllerはソースのivarnumberarrayを含み、もう1つBSotherViewControllerはターゲットとしてivarを受け取る必要があります。 この質問では、目的の結果を生成する1つの方法が提供されました。提案は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はivarを受信して​​いません。

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)
4

1 に答える 1

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

セグエから宛先コントローラを取得し、そのプロパティに値を割り当てる必要があります。リンクした回答は、これを行う方法を示しています。

于 2013-03-14T08:27:50.930 に答える