我在这个网站上看到了类似的问题,我相信我已经遵循了建议,但是我似乎无法让它正常工作。这是第一个控制器 .m 的示例
@interface FirstController : UITableViewController <UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>
{
NSMutableDictionary * routesDictionary;
}
@property (nonatomic, strong) NSMutableDictionary * routesDictionary;
和 .h
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"SelectRouteSegue"])
{
SelectRouteController * selectRoutes = [[SelectRouteController alloc]init];
selectRoutes.diction = self.routesDictionary;
UINavigationController *navController= [segue destinationViewController];
NSLog(@"my diction:%@", selectRoutes.diction);
//At this point I see that the values are indeed in selectRoutes.diction
navController = [[UINavigationController alloc] initWithRootViewController:selectRoutes];
}
}
在第二个控制器.h中
@interface SecondController : UITableViewController
{
NSMutableDictionary * diction;
}
@property (nonatomic, strong) NSMutableDictionary * diction;
第二个控制器 .m
@synthesize diction;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Select";
NSLog(@"New Data: %@",diction);
//I constantly get null values.
routesArray = [diction objectForKey:@"routes"];
}
第二个控制器中 NSMuableDictionary 词典的空值是什么原因?
你的 prepareForSegue 在几个方面是错误的。
调用该方法时,已创建新的视图控制器。它是 segue 的"destinationViewController"。您不应该分配/初始化 SelectRouteController 的新实例。相反,您应该要求 segue 提供指向 destinationViewController 的指针,并将其转换为正确的类型:
SelectRouteController * selectRoutes =
(SelectRouteController *) segue. destinationViewController;
导航控制器也是如此。假设您的 segue 是推送 segue,segue 机制将负责将新的视图控制器推送到导航控制器的堆栈上。您不应该像在以下行中那样创建新的视图控制器:
navController = [[UINavigationController alloc] initWithRootViewController:selectRoutes];
除非我误解了,并且您的 segue 是其他类型的 segue,并且您正在链接到新的导航控制器。
如果是这种情况,那么您需要解释故事板的结构以及 segue 如何链接。
无论如何,您的 prepareForSegue 方法都搞砸了。