今天我學會了如何在視圖控制器之間傳遞數據,無法使用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];
}
}];
}
任何幫助,這並瞭解爲什麼我不能做到這一點我想要的方式來將不勝感激謝謝!
謝謝你的幫助。我將不得不爲現在的設置做好準備,稍後將會探討NSUserDefaults。 – user3117509
不客氣! –