为什么数据给我空值?
Why does data give me null?
我正在尝试将 NSDictionary
从 TableViewController
传递给 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
然后我尝试在 ViewController.m
中记录 data
:
@implementation SeeCardVC
@synthesize data;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"Info: %@", data);
}
但它只给我 null :(
我究竟做错了什么?
让我们看看您的代码在这里做了什么:
// 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];
您创建了一个控制器并为其分配了数据,然后不再使用它,而是从情节提要中创建了一个新控制器,您没有添加数据并呈现它。
基本上,你创建了两个,给一个设置数据,然后呈现另一个。
看到问题了吗?
我正在尝试将 NSDictionary
从 TableViewController
传递给 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
然后我尝试在 ViewController.m
中记录 data
:
@implementation SeeCardVC
@synthesize data;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"Info: %@", data);
}
但它只给我 null :( 我究竟做错了什么?
让我们看看您的代码在这里做了什么:
// 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];
您创建了一个控制器并为其分配了数据,然后不再使用它,而是从情节提要中创建了一个新控制器,您没有添加数据并呈现它。
基本上,你创建了两个,给一个设置数据,然后呈现另一个。
看到问题了吗?