2017-06-19 78 views
-1

我想从一个UIViewController传递数组到其他viewcontroller。 我尝试使用属性(viewcontroller1.array)。 我试过通知(发布通知和添加观察者)。 我试过NSUserDefaults 所有的都返回一个空数组。从一个UIViewController传递数组到其他ViewController返回null

+4

添加您尝试代码。 –

+0

[视图控制器之间传递数据]的可能的复制(https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) –

回答

1
中的ViewController 1

从阵列是通过

@interface demopreseViewController() { 
    NSArray* nameArr; 
} 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin",nil]; 
    NSLog(@"%@",nameArr); 

} 

- (IBAction)PresentAction:(id)sender { 
    ViewController2 *aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"aa"]; 
    aviewcontroller.array1 = nameArr; 
    [self presentViewController:aviewcontroller animated:YES completion:nil]; 
} 

在的viewController 2其中阵列是

@property (strong, nonatomic) NSArray *array1; 

下ViewController2.m

传递

ViewController2.h下

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"parsed%@",_array1); 
} 

阵列数据将被传递

+0

.I尝试执行上述类型的步骤已经但仍空数组的IAM获得。是强制给这样的“ViewController2 * aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@”aa“];”。 ?当我给上面的行iam得到无法识别的选择器发送到实例错误。当然,我已经给我的故事板指定的名称标识符。我有许多故事板文件在项目中?如果我给self.storyboard,它会有所作为吗? – sampath

+0

您是否尝试使用故事板的标识符像下面 UIStoryboard *故事板= [UIStoryboard storyboardWithName:@ “MainStoryboard” 束:无]; –

+0

@sampath,'viewConrtoller2'是视图控制器类的名称,所以你需要给你的。 – vaibhav

0

定义属性为您的阵列

@Property(nonatomic, retain) NSArray *arrayName; 

其次

ViewControllerClassName *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"StoryboardIdentifier"]; 
controller.array = arrayName; 
相关问题