调用故事板数据 - 目标C.

时间:2014-04-17 23:08:59

标签: ios objective-c

我正在尝试从NSMutableArray之一访问另一个ViewControllers

这是我正在做的事情:

a.h

#import "b.h"
.....
@property(nonatomic,strong) b *viewControllers;

a.m

- (void)viewDidLoad
{
    [super viewDidLoad]; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    _viewControllers = [storyboard instantiateViewControllerWithIdentifier:@"createChallengeInfo"];
    .....code....
}
....code....
-(IBAction)sendVideo:(id) sender
{
    NSLog(@"%@",self.viewControllers.selectedCells);
}

现在logging我遇到错误EXC_BAD_ACCESS (code=1,address =0x13765350)

这是我的另一个ViewController

b.h

...code....
@property (strong, nonatomic) NSMutableArray *selectedCells; <-- this is the array I want to access
...code...

b.m

#import "a.h"
...import...
@interface b ()

@end

@implementation b
- (void)viewDidLoad
{

    [super viewDidLoad];

    /* INITIATE VARIABLE */

    _selectedCells = [[NSMutableArray alloc] init];
    ....code....
}
...code...

所以我的问题是访问selectedCells。但首先为什么我得到EXC_BAD_ACCESS?其次,在我得到error之前,它会列出ViewController,并将变量selectedCells显示为nil。

建议,想法?

1 个答案:

答案 0 :(得分:1)

如果“b”嵌入在“a”的容器视图中,则在“a”实例化时它已经被实例化。因此,当您使用instantiateViewControllerWithIdentifier:时,您正在创建一个新实例,而不是您在屏幕上拥有的实例。你的viewDidLoad应该是这样的,

- (void)viewDidLoad
{
    [super viewDidLoad]; 
    _viewControllers = self.childViewControllers[0];
    .....code....
}

获取对嵌入式视图控制器的引用的另一种方法是prepareForSegue,它将在实例化“a”控制器后立即调用。你的“b”控制器将是segue.destinationViewController。