将 NSMutableDictionary 从一个视图传递到另一个视图
Pass NSMutableDictionary from one view to another
我正在尝试将数据从一个 ViewController 传递到另一个 ViewController。
Firstviewcontroller.m
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
{
InfoViewController *vc = [segue destinationViewController];
[vc setAppointmentDictionary:dict];
}
}
Secondviewcontroller.h
@property (strong, nonatomic) NSMutableDictionary *AppointmentDictionary;
Secondviewcontroller.m
@synthesize AppointmentDictionary;
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"dict===>%@",AppointmentDictionary); // This gives me null
}
请帮助我。我做错了什么。
我在 viewwillappear
中得到 AppointmentDictionary = (null)
。
虽然我正在做这个的正确副本。
您正在创建 InfoViewController 实例,而不是推送该视图控制器。您在此处执行 "performSegueWithIdentifier" 将创建新实例,因此您的第一个实例 "info" 不会通过此处。你可以用其他方式做。
InfoViewController *info = [[InfoViewController alloc] init];
info.AppointmentDictionary = [dict mutableCopy];
[self.navigationController pushViewController: info animated:true];
选项 2:
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender: [dict mutableCopy]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"NavDashBoardToInfo"]) {
InfoViewController *viewController = (InfoViewController *)segue.destinationViewController;
viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
}
}
在 prepareforsegue 方法中,您必须像上面提到的那样传递值。
注意:确保你的目标视图控制器是 InfoViewController
试试这个:
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict
{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:dict];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
InfoViewController *info = segue.destinationViewController;
info.AppointmentDictionary = sender;
}
如果您使用 performsegue
进行导航,那么您可以使用以下方法在 ViewControllers
之间传输数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
{
InfoViewController *info = [segue destinationViewController];
info.AppointmentDictionary = "Value you want to pass";
}
}
试试这个
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:@selector(setAppointmentDictionary:)]) {
[segue.destinationViewController performSelector:@selector(setAppointmentDictionary:)
withObject:dict];
}
}
如果您使用的是 performSegueWithIdentifier,请在函数中提供正文。不要使用代码创建任何新实例并在故事板中提供 segue -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"YourController_Segue"]) {
YourController *viewController = (YourController *)segue.destinationViewController;
viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
}
}
我正在尝试将数据从一个 ViewController 传递到另一个 ViewController。
Firstviewcontroller.m
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
{
InfoViewController *vc = [segue destinationViewController];
[vc setAppointmentDictionary:dict];
}
}
Secondviewcontroller.h
@property (strong, nonatomic) NSMutableDictionary *AppointmentDictionary;
Secondviewcontroller.m
@synthesize AppointmentDictionary;
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"dict===>%@",AppointmentDictionary); // This gives me null
}
请帮助我。我做错了什么。
我在 viewwillappear
中得到 AppointmentDictionary = (null)
。
虽然我正在做这个的正确副本。
您正在创建 InfoViewController 实例,而不是推送该视图控制器。您在此处执行 "performSegueWithIdentifier" 将创建新实例,因此您的第一个实例 "info" 不会通过此处。你可以用其他方式做。
InfoViewController *info = [[InfoViewController alloc] init];
info.AppointmentDictionary = [dict mutableCopy];
[self.navigationController pushViewController: info animated:true];
选项 2:
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender: [dict mutableCopy]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"NavDashBoardToInfo"]) {
InfoViewController *viewController = (InfoViewController *)segue.destinationViewController;
viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
}
}
在 prepareforsegue 方法中,您必须像上面提到的那样传递值。 注意:确保你的目标视图控制器是 InfoViewController
试试这个:
-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict
{
[self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:dict];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
InfoViewController *info = segue.destinationViewController;
info.AppointmentDictionary = sender;
}
如果您使用 performsegue
进行导航,那么您可以使用以下方法在 ViewControllers
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
{
InfoViewController *info = [segue destinationViewController];
info.AppointmentDictionary = "Value you want to pass";
}
}
试试这个
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:@selector(setAppointmentDictionary:)]) {
[segue.destinationViewController performSelector:@selector(setAppointmentDictionary:)
withObject:dict];
}
}
如果您使用的是 performSegueWithIdentifier,请在函数中提供正文。不要使用代码创建任何新实例并在故事板中提供 segue -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"YourController_Segue"]) {
YourController *viewController = (YourController *)segue.destinationViewController;
viewController.AppointmentDictionary = (NSMutableDictionary *)sender;
}
}