我有一些重複的代碼我試圖重構:視圖控制器的動態類型
if (_currentIndex >= [_questions count] - 1) {
[patient setDate:[NSDate date]];
ConfirmationViewController *confirmation = [self.storyboard instantiateViewControllerWithIdentifier:@"Confirmation"];
[confirmation setPatient:patient];
[confirmation setQuestions: _questions];
[self.navigationController pushViewController:confirmation animated:YES];
} else if ([[_questions objectAtIndex:_currentIndex + 1] isEqualToString:@"date"]) {
DateViewController *dateView = [self.storyboard instantiateViewControllerWithIdentifier:@"Date"];
[dateView setPatient:patient];
[dateView setQuestions: _questions];
[dateView setCurrentIndex: _currentIndex + 1];
[self.navigationController pushViewController:dateView animated:YES];
} else {
QuestionViewController *nextQuestion = [self.storyboard instantiateViewControllerWithIdentifier:@"Question"];
[nextQuestion setCurrentIndex:_currentIndex + 1];
[nextQuestion setPatient:patient];
[nextQuestion setQuestions: _questions];
[self.navigationController pushViewController:nextQuestion animated:YES];
}
我想聲明一個變量nextView
這可以是一個ConfirmationViewController,DateViewController,或QuestionViewController,因爲所有其中的步驟有setPatient:patient
,[self.navigationController pushViewController...]
和[setQuestions:_questions]
,並且在運行特定於代碼段的代碼之後調用該塊,但由於它們都是不同的類型,我無法弄清楚如何聲明這個'view'變量(我主要是JS背景,所以我已經習慣了var
-在頂部!)
其中之一(ConfirmationViewController)是一個tableViewController - 是否有可能讓這個共享基類? – thisAnneM
@thisAnneM你可以使用一個通用的協議,看看編輯。 – dasblinkenlight