2017-01-06 2 views
0

あるViewControllerから別のViewControllerにデータを渡そうとしています。NSMutableDictionaryを別のビューに渡す

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 
} 

私を助けてください。私は間違っている。

AppointmentDictionary = (null)viewwillappearになります。 私はこれを正しくコピーしていますが。

+2

これは何度か聞かれていますが、利用可能なSOの投稿を見ましたか? –

+0

ここで間違ったことがたくさんありますが、どのようにvcを初期化するかは間違っていますし、segueを呼び出すために初期化する必要はなく、 'prepareForSegue'からデータを渡す必要があります。最後に関数とプロパティ名を小文字にしてください。 – Tj3n

+0

@ Tj3nコメントありがとうございます。私はプロパティと関数名を変更します。 prepareForSegueからデータを渡す必要がありますか?私は理解していなかった。 –

答えて

2

試着する

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([segue.destinationViewController respondsToSelector:@selector(setAppointmentDictionary:)]) { 
    [segue.destinationViewController performSelector:@selector(setAppointmentDictionary:) 
              withObject:dict]; 
} 
} 
1

InfoViewControllerインスタンスを作成していて、そのビューコントローラをプッシュしていません。ここで新しいインスタンス「performSegueWithIdentifier」を作成すると、最初のインスタンス「info」はここを通過しません。あなたは他の方法で行うことができます。

InfoViewController *info = [[InfoViewController alloc] init]; 
info.AppointmentDictionary = [dict mutableCopy]; 
[self.navigationController pushViewController: info animated:true]; 

オプション2:あなたは、上記のように値を渡す必要がprepareforsegue方法で

-(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; 
    } 
} 

。 注:お使いの先ビュー・コントローラはInfoViewController

+0

'[[InfoViewController alloc] init];'これは空のviewcontrollerになります、私はむしろsegueを使用するように彼を指摘すると良いです – Tj3n

+0

答えはこれを見てください。 –

2

であることを確認して試してみてください、この1:

-(void)NavigateToAnotherScreen:(NSMutableDictionary*)dict 
{ 
    [self performSegueWithIdentifier:@"NavDashBoardToInfo" sender:dict]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     InfoViewController *info = segue.destinationViewController; 
     info.AppointmentDictionary = sender; 
} 
1

あなたは、あなたがViewControllers間でデータを転送するための方法の下に使用することができ、ナビゲーションのためのperformsegueを使用している場合

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"]) 
    { 
     InfoViewController *info = [segue destinationViewController]; 
     info.AppointmentDictionary = "Value you want to pass"; 
    } 
} 
1

performSegueWithIdentifierを使用している場合は、body in functionを指定します。コードを使用して新しいインスタンスを作成せず、ストーリーボードにsegueを指定しないでください -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"YourController_Segue"]) { 
     YourController *viewController = (YourController *)segue.destinationViewController; 
     viewController.AppointmentDictionary = (NSMutableDictionary *)sender; 
    } 
} 
関連する問題