2013-12-16 23 views
0

我在這裏有關於在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。

謝謝。

+1

爲什麼不直接使用的屬性? –

+1

本教程鏈接可能會對您有所幫助:http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ –

回答

0

嘗試這樣的,希望這有助於ü:)

SecondViewController.h

@interface SecondViewController : UIViewController 
{ 
    NSDictionary *passedDataContainer; 
} 

@property (nonatomic, retain) NSDictionary *data; 


SecondViewController.m


@synthesize data; //just synthesize it 


和你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.data = passedData; //[secondVC setData:passedData];//change this 
[self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self]; 
} 


0

如果您想將數據從secondviewcontroller傳遞到第一個視圖控制器,那麼您可以使用CUSTOM DELEGATE,使用custome委託您可以將數據從secondviewcintroller傳遞到firstviewcontroller。

This鏈接可能對您有幫助。 這是second鏈接瞭解代表。

0

有很多方法可以實現您的解決方案。

  1. NSUserDefaults。
  2. 代表(自定義)。

您的問題:Use the @PRoperty (Strong) reference to the NSDictionary and refer it from FirstViewController with the proper alloc of instance of SecondViewController.

0

形成了你的解釋,你正在使用storyboard segue儘快過渡到FirstViewControllerSecondViewController。您需要通過prepareForSegue方法而不是showSecondViewController方法傳遞數據。

試試這個

FirstViewController.m 

-(void)showSecondViewController{ 

    [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"SecondViewControllerSegue"]){ 
    NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init]; 
    [passedData setObject:@"First Object" forKey:@"first_obj"]; 
    [passedData setObject:@"Second Object" forKey:@"second_obj"]; 

    SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController; 
    [secondVC setData:passedData]; 
} 

SecondViewController.h

@property(strong, nonatomic) NSDictionary *data; 

SecondViewController.m

-(void)usePassedData{ 
    NSLog(@"Display Passed Data from FirstViewController: %@",data); 
}