我在這裏有關於在NSDictionary上丟失數據的問題(或者我試圖在視圖控制器之間傳遞的任何數據類型)。我正在傳遞數據,因爲我將在下面的代碼中顯示。但是在傳遞並再次訪問數據之後,我收到了一個對象null
。數據在功能之間傳遞後丟失
FirstViewController.m
-(void)showSecondViewController{
SecondViewController *secondVC = [SecondViewController new];
NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
[passedData setObject:@"First Object" forKey:@"first_obj"];
[passedData setObject:@"Second Object" forKey:@"second_obj"];
[secondVC setData:passedData];
[self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}
在我SecondViewController.h
@interface SecondViewController : UIViewController
{
NSDictionary *passedDataContainer;
}
-(void)setData(NSDictionary *)data;
而且在SecondViewController.m
-(void)setData(NSDictionary *)data{
passedDataContainer = data;
}
從這我假設我有我的NSDictionary
從我FirstViewController
,但是當我使用它,例如說。 ..
-(void)usePassedData{
NSLog(@"Display Passed Data from FirstViewController: %@",passedDataContainer);
}
在此代碼片段,輸出將是
Display Passed Data from FirstViewController: (null)
我希望我已經解釋的情況很好。我檢查了一些可能的修復方法,例如我在這裏找到的這個帖子--->iOS NSDictionary pass through function。如果我知道這是什麼原因,我會很高興,我是Objective-C的新手。
P.S.我正在使用ARC。
謝謝。
爲什麼不直接使用的屬性? –
本教程鏈接可能會對您有所幫助:http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ –