0

2つのViewControllerがあります。1つBSViewControllerはソースのivarnumberarrayを 含み、もう1つBSotherViewControllerはターゲットとしてivarを受け取る必要があります。(BSViewControllerセグエのボタンがありますBSotherViewController。)

の2つのivarの値にアクセスするにはどうすればよい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

以下の問題はaview.number、25ではなく0であるということです。そしてaview.arraynullです。

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

2 に答える 2

1

BSViewControllerfromをインスタンス化するBSOtherViewControllerと、initメソッドが呼び出されます。値はで設定されviewDidLoadBSViewControllerビューが実際にロードされるまでそのメソッドは呼び出されません。

initメソッドをオーバーライドして、値を設定してみてください

- (id)init {

if (self = [super init]) {
    //Set values
     NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
     self.array = _array;
     self.number = 25;
}
return self;
}
于 2013-03-12T11:51:30.703 に答える
0

BSViewController のviewDidLoadメソッドを次のようにinitに置き換えます。

- (id)init {

if (self = [super init]) {

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

}
return self;
}
于 2013-03-12T12:22:30.753 に答える