2014-01-07 33 views
0

今天我學會了如何在視圖控制器之間傳遞數據,無法使用prepareForSegue方法將數據傳遞給另一個視圖控制器。如果該視圖控制器不是立即被關聯到

我有3個視圖控制器,它們都使用推塞格斯,他們被順序用戶訪問:註冊視圖控制器>驗證 - 視圖 - 控制器>引腳提交 - 視圖 - 控制器

不管怎麼說,今天早些時候,我能成功地將數據從註冊視圖控制器傳遞到驗證視圖控制器。

我然後意識到我需要通過,被上註冊視圖控制器產生至引腳提交視圖控制器的一些數據。我嘗試使用正常的prepareForSegue方法實現,並且無法在過去一小時內使其工作。

所以我很沮喪,並進行設置,從而使數據從註冊視圖控制器通過,以覈查視圖控制器,然後到引腳提交視圖控制器。即使驗證視圖控制器不需要或沒有任何使用我傳遞的數據。

果然,它工作完美!但令人沮喪的是,我不明白爲什麼我不能使用我的常規方法,並且當驗證視圖控制器甚至不會使用數據時,將數據從SignUp View Controller直接傳遞到Pin提交視圖控制器。

是因爲我使用prepareForSegue方法嗎?

這是我一直在使用的一個例子。這是我prepareForSegue實現在我的「發送」視圖控制器的主要文件:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:self { 


if ([segue.identifier isEqualToString:@"signupToVerificationSegue"]) { 


    VerificationViewController *verifyViewController = segue.destinationViewController; 


    PinSubmitViewController *pinViewController = [[PinSubmitViewController alloc]init]; 


    verifyViewController.uniqueVerificationCode = _uniqueVerificationCode; 
    verifyViewController.userSubmittedUsername = _userSubmittedUsername; 

    pinViewController.userSubmittedUsername = _userSubmittedUsername; 

} 
} 

然後在這裏是迷上了一個UIButton並負責又名調用兩個prepareForSegue執行實際SEGUE的IBAction爲方法implemetation上面我們實現的方法和performSegueWithIdentifier方法:

- (IBAction)didTapSignup:(id)sender { 

NSString *user = [_usernameEntry text]; 
NSString *pass = [_passwordEntry text]; 
NSString *email = [_emailEntry text]; 

self.userSubmittedUsername = user; 

int randomCode = arc4random() % 9000 + 1000; 

NSString *uniqueVerificationCode = [[NSString alloc]initWithFormat:@"%d", randomCode]; 

self.uniqueVerificationCode = uniqueVerificationCode; 

PinSubmitViewController *pinViewController = [[PinSubmitViewController alloc]init]; 
pinViewController.userSubmittedUsername = _userSubmittedUsername; 

NSLog(@"%@", _userSubmittedUsername); 
NSLog(@"%@", user); 
NSLog(@"%@", pass); 
NSLog(@"%@", email); 
NSLog(@"Random 4 Digit Code: %@", uniqueVerificationCode); 

if ([user length] < 4 || [pass length] < 4) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Username and Password must both be at least 4 characters long." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
    [alert show]; 
} else if ([email length] < 8) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Please enter your email address." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
    [alert show]; 
} else { 

    [_activityIndicator startAnimating]; 

    PFUser *newUser = [PFUser user]; 
    newUser.username = user; 
    newUser.password = pass; 
    newUser.email = email; 


    NSLog(@"SEE IF THE PROPERTY WORKED: %@", self.uniqueVerificationCode); 
    newUser[@"verificationCode"] = self.uniqueVerificationCode; 

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
     [_activityIndicator stopAnimating]; 
     if (error) { 
      NSString *errorString = [[error userInfo] objectForKey:@"error"]; 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
      [alert show]; 
     } else { 

NSLog(@"No errors in signup process"); 

UIStoryboardSegue *segue = [[UIStoryboardSegue alloc]init]; 

[self prepareForSegue:segue sender:self]; 

[self performSegueWithIdentifier:@"signupToVerificationSegue" sender:self]; 


} 
}]; 
} 

任何幫助,這並瞭解爲什麼我不能做到這一點我想要的方式來將不勝感激謝謝!

回答

2

真的發生在prepareForSegue方法是什麼,它通知視圖控制器,一個SEGUE即將被執行。

所以,你可以使用segue的對象訪問destinationViewController,你可以ofcourse通過訪問性能的一些數據傳遞給它。

你想實現的是將數據傳遞給一些甚至沒有分配到內存中的ViewController。 這行代碼:

PinSubmitViewController *pinViewController = [[PinSubmitViewController alloc]init]; 

將創建的PinSubmitViewController一個新的對象,無關與推塞格斯。

所以,你要麼數據傳遞到VerificationViewController,然後把它傳遞給PinSubmitViewController也可以使用NSUserDefaults保存和訪問數據(因爲我可以看到的數據並不多)。你也可以使用單身人士。

+1

謝謝你的幫助。我將不得不爲現在的設置做好準備,稍後將會探討NSUserDefaults。 – user3117509

+0

不客氣! –

1

你並不需要創建內部prepareForSegue方法的新的UIViewController。你創建的那個 - PinSubmitViewController - 在prepareForSegue方法的末尾被取消分配,因爲ARC沒有看到任何強引用。

在你的情況我會通過數據直通VerificationViewController或使用NSUserDefaults

P.S.您也不需要手動呼叫[self prepareForSegue:segue sender:self] - 它會在執行segue的過程中自動調用。

+0

感謝您的幫助,並感謝您的PS評論。我清理了我的代碼並刪除了prepareForSegue的不必要的方法調用。 – user3117509

相關問題