为什么数据会给我null?

时间:2016-01-24 18:05:37

标签: ios objective-c tableview viewcontroller

我正在尝试将NSDictionaryTableViewController传递给ViewController。在我的TableViewController.m。我有这段代码导航到ViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];
    NSLog(@"Info: %@", objectDict);
    // Pass object to new page
    //UIViewController * vc = [[UIViewController alloc] init];
    //[self presentViewController:vc animated:YES completion:nil];

    SeeInfoVC *controller = [[SeeInfoVC alloc] init];
    controller.data = objectDict;

    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"SeeInfoVC"];
    [self presentViewController:vc animated:YES completion:nil];

ViewController.h我有:

@interface SeeCardVC : UIViewController
{
    NSDictionary *data;
}
@property (nonatomic, retain)NSDictionary *data;


@end

然后我尝试在data中记录ViewController.m

@implementation SeeCardVC
@synthesize data;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"Info: %@", data);

}

但它只给我null :( 我究竟做错了什么?

1 个答案:

答案 0 :(得分:2)

让我们看看你的代码在这里做了什么:

// Create new controller, assign objectDict to data property
SeeInfoVC *controller = [[SeeInfoVC alloc] init];
controller.data = objectDict;

//Get Storyboard name
NSString * storyboardName = @"Main";
//Get Storyboard (BTW, you can do this with self.storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
//Instantiate NEW controller
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"SeeInfoVC"];
//Present NEW controller
[self presentViewController:vc animated:YES completion:nil];

您创建了一个控制器并为其分配了数据,然后又不再使用它,而是从故事板创建了一个新的控制器,您没有添加数据并将其显示出来。

基本上,你创建了两个,将数据设置为一个并呈现另一个。

看到问题?