如何将方法移出应用程序委托?
How to move methods out of app delegate?
我正在尝试将方法移出应用程序委托,而是使用单独的视图控制器来符合协议。
旧的应用委托是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
return YES;
}
/* Set up the question view controller with a question and push onto the nav stack
*/
- (void)prepareForQuestion:(VRQuestion *)question animated:(BOOL)animated;
{
VRQuestionViewController *qvc = (VRQuestionViewController *)[self.navController.storyboard instantiateViewControllerWithIdentifier:@"QuestionViewController"];
qvc.delegate = self;
qvc.question = question;
[self.navController pushViewController:qvc animated:animated];
}
/* Delegate that gets called every time a question is answered. This loads the next question and pushes it onto the nav stack.
It also pushes a pointer to the question and result to a linked-list called firstAnswer->nextAnswer->nextAnswer, etc.
When the next question returns nil we know we've finished as there are no more questions and log linked-list to console.
*/
- (void)questionViewController:(VRQuestionViewController *)controller didAnswerQuestion:(VRQuestion *)question withResult:(BOOL)result
{
VRQuestion *nextQuestion = nil;
if (result == YES) {
nextQuestion = question.nextYesQuestion;
} else if (result == NO) {
nextQuestion = question.nextNoQuestion;
}
[self.qnaire pushAnswerResult:result forQuestion:question];
// Handle no more questions
if(nextQuestion) {
[self prepareForQuestion:nextQuestion animated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Complete"
message:@"You completed the questionnaire"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self.qnaire logAnswers];
}
}
我正在尝试将这些方法移至 InitialViewController ,但按钮什么都不做?
@implementation InitialViewController
- (IBAction)buttonPress:(UIButton *)sender {
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
//self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
}
提前致谢。
InitialViewController 按钮应该调用 VRQuestionViewController(如下所示)
@interface VRQuestionViewController ()
@end
@implementation VRQuestionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.label.text = self.question.text;
self.title = [self.question.identifier uppercaseString];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)yesPressed:(id)sender
{
if (self.delegate != nil) {
if ([self.delegate respondsToSelector:@selector(questionViewController:didAnswerQuestion:withResult:)]) {
[self.delegate questionViewController:self didAnswerQuestion:self.question withResult:YES];
}
}
}
- (IBAction)noPressed:(id)sender
{
if (self.delegate != nil) {
if ([self.delegate respondsToSelector:@selector(questionViewController:didAnswerQuestion:withResult:)]) {
[self.delegate questionViewController:self didAnswerQuestion:self.question withResult:NO];
}
}
}
@end
我当前的 AppDelegate:
#import <UIKit/UIKit.h>
#import "VRQuestionnaire.h"
#import "VRQuestionViewController.h"
@interface VRAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) VRQuestionnaire *qnaire;
@end
#import "VRAppDelegate.h"
#import "VRQuestionViewController.h"
@implementation VRAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
return YES;
}
和 InitialViewController:
#import "VRAppDelegate.h"
#import "VRQuestionViewController.h"
@implementation VRAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
return YES;
}
#import "InitialViewController.h"
@interface InitialViewController ()
@end
@implementation InitialViewController
- (IBAction)buttonPress:(UIButton *)sender {
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
}
/* Set up the question view controller with a question and push onto the nav stack
*/
- (void)prepareForQuestion:(VRQuestion *)question animated:(BOOL)animated;
{
VRQuestionViewController *qvc = (VRQuestionViewController *)[self.navController.storyboard instantiateViewControllerWithIdentifier:@"QuestionViewController"];
qvc.delegate = self;
qvc.question = question;
[self.navController pushViewController:qvc animated:animated];
}
/* Delegate that gets called every time a question is answered. This loads the next question and pushes it onto the nav stack.
It also pushes a pointer to the question and result to a linked-list called firstAnswer->nextAnswer->nextAnswer, etc.
When the next question returns nil we know we've finished as there are no more questions and log linked-list to console.
*/
- (void)questionViewController:(VRQuestionViewController *)controller didAnswerQuestion:(VRQuestion *)question withResult:(BOOL)result
{
VRQuestion *nextQuestion = nil;
if (result == YES) {
nextQuestion = question.nextYesQuestion;
} else if (result == NO) {
nextQuestion = question.nextNoQuestion;
}
[self.qnaire pushAnswerResult:result forQuestion:question];
// Handle no more questions
if(nextQuestion) {
[self prepareForQuestion:nextQuestion animated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Complete"
message:@"You completed the questionnaire"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self.qnaire logAnswers];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
从导航控制器按住 CTRL 并拖动以创建 InitialViewController 根视图控制器。
按住 CTRL 从按钮拖动到 VRQuestionViewController。
在模拟器中,当按下按钮 VRQuestionViewController 时显示没有问题或响应 Y/N
对不起,我正在做一个真正的猪耳朵!
有问题的代码似乎是 https://github.com/rwarrender/DynamicQuestions
中列出的 Github 上的示例代码
为了可读性,相当多的逻辑发生在应用程序委托中。应用委托初始化 VRQuestionnaire
实例并设置问题。然后它在导航控制器上设置第一个问题视图控制器。每个 VRQuestionViewController
都已配置,因此应用委托也是 VRQuestionViewController
的委托。一旦回答了一个问题,它就会回调 questionViewController:didAnswerQuestion:withResult:
,应用代理将结果推送到调查问卷的答案列表中,然后沿着问题树移动以显示下一个问题。
正如 Wain 所建议的那样,如果您想将其扩展到多个示例,您可能会将应用程序删除代码移动到控制器中 class。您的初始视图控制器 class 有问题,因为所有内容都包含在导航控制器中,因此您的导航控制器应该是情节提要中的初始视图控制器,您的 InitialViewController
class 为导航控制器的根视图控制器。您可以通过右键单击从情节提要中的导航控制器拖动到 InitialViewController
.
希望对您有所帮助!
我正在尝试将方法移出应用程序委托,而是使用单独的视图控制器来符合协议。 旧的应用委托是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
return YES;
}
/* Set up the question view controller with a question and push onto the nav stack
*/
- (void)prepareForQuestion:(VRQuestion *)question animated:(BOOL)animated;
{
VRQuestionViewController *qvc = (VRQuestionViewController *)[self.navController.storyboard instantiateViewControllerWithIdentifier:@"QuestionViewController"];
qvc.delegate = self;
qvc.question = question;
[self.navController pushViewController:qvc animated:animated];
}
/* Delegate that gets called every time a question is answered. This loads the next question and pushes it onto the nav stack.
It also pushes a pointer to the question and result to a linked-list called firstAnswer->nextAnswer->nextAnswer, etc.
When the next question returns nil we know we've finished as there are no more questions and log linked-list to console.
*/
- (void)questionViewController:(VRQuestionViewController *)controller didAnswerQuestion:(VRQuestion *)question withResult:(BOOL)result
{
VRQuestion *nextQuestion = nil;
if (result == YES) {
nextQuestion = question.nextYesQuestion;
} else if (result == NO) {
nextQuestion = question.nextNoQuestion;
}
[self.qnaire pushAnswerResult:result forQuestion:question];
// Handle no more questions
if(nextQuestion) {
[self prepareForQuestion:nextQuestion animated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Complete"
message:@"You completed the questionnaire"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self.qnaire logAnswers];
}
}
我正在尝试将这些方法移至 InitialViewController ,但按钮什么都不做?
@implementation InitialViewController
- (IBAction)buttonPress:(UIButton *)sender {
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
//self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
}
提前致谢。
InitialViewController 按钮应该调用 VRQuestionViewController(如下所示)
@interface VRQuestionViewController ()
@end
@implementation VRQuestionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.label.text = self.question.text;
self.title = [self.question.identifier uppercaseString];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)yesPressed:(id)sender
{
if (self.delegate != nil) {
if ([self.delegate respondsToSelector:@selector(questionViewController:didAnswerQuestion:withResult:)]) {
[self.delegate questionViewController:self didAnswerQuestion:self.question withResult:YES];
}
}
}
- (IBAction)noPressed:(id)sender
{
if (self.delegate != nil) {
if ([self.delegate respondsToSelector:@selector(questionViewController:didAnswerQuestion:withResult:)]) {
[self.delegate questionViewController:self didAnswerQuestion:self.question withResult:NO];
}
}
}
@end
我当前的 AppDelegate:
#import <UIKit/UIKit.h>
#import "VRQuestionnaire.h"
#import "VRQuestionViewController.h"
@interface VRAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) VRQuestionnaire *qnaire;
@end
#import "VRAppDelegate.h"
#import "VRQuestionViewController.h"
@implementation VRAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
return YES;
}
和 InitialViewController:
#import "VRAppDelegate.h"
#import "VRQuestionViewController.h"
@implementation VRAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
return YES;
}
#import "InitialViewController.h"
@interface InitialViewController ()
@end
@implementation InitialViewController
- (IBAction)buttonPress:(UIButton *)sender {
self.qnaire = [[VRQuestionnaire alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"set1"
ofType:@"json"]];
self.navController = (UINavigationController *)self.window.rootViewController;
[self prepareForQuestion:self.qnaire.firstQuestion animated:NO];
}
/* Set up the question view controller with a question and push onto the nav stack
*/
- (void)prepareForQuestion:(VRQuestion *)question animated:(BOOL)animated;
{
VRQuestionViewController *qvc = (VRQuestionViewController *)[self.navController.storyboard instantiateViewControllerWithIdentifier:@"QuestionViewController"];
qvc.delegate = self;
qvc.question = question;
[self.navController pushViewController:qvc animated:animated];
}
/* Delegate that gets called every time a question is answered. This loads the next question and pushes it onto the nav stack.
It also pushes a pointer to the question and result to a linked-list called firstAnswer->nextAnswer->nextAnswer, etc.
When the next question returns nil we know we've finished as there are no more questions and log linked-list to console.
*/
- (void)questionViewController:(VRQuestionViewController *)controller didAnswerQuestion:(VRQuestion *)question withResult:(BOOL)result
{
VRQuestion *nextQuestion = nil;
if (result == YES) {
nextQuestion = question.nextYesQuestion;
} else if (result == NO) {
nextQuestion = question.nextNoQuestion;
}
[self.qnaire pushAnswerResult:result forQuestion:question];
// Handle no more questions
if(nextQuestion) {
[self prepareForQuestion:nextQuestion animated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Complete"
message:@"You completed the questionnaire"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self.qnaire logAnswers];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
从导航控制器按住 CTRL 并拖动以创建 InitialViewController 根视图控制器。 按住 CTRL 从按钮拖动到 VRQuestionViewController。
在模拟器中,当按下按钮 VRQuestionViewController 时显示没有问题或响应 Y/N
对不起,我正在做一个真正的猪耳朵!
有问题的代码似乎是 https://github.com/rwarrender/DynamicQuestions
中列出的 Github 上的示例代码为了可读性,相当多的逻辑发生在应用程序委托中。应用委托初始化 VRQuestionnaire
实例并设置问题。然后它在导航控制器上设置第一个问题视图控制器。每个 VRQuestionViewController
都已配置,因此应用委托也是 VRQuestionViewController
的委托。一旦回答了一个问题,它就会回调 questionViewController:didAnswerQuestion:withResult:
,应用代理将结果推送到调查问卷的答案列表中,然后沿着问题树移动以显示下一个问题。
正如 Wain 所建议的那样,如果您想将其扩展到多个示例,您可能会将应用程序删除代码移动到控制器中 class。您的初始视图控制器 class 有问题,因为所有内容都包含在导航控制器中,因此您的导航控制器应该是情节提要中的初始视图控制器,您的 InitialViewController
class 为导航控制器的根视图控制器。您可以通过右键单击从情节提要中的导航控制器拖动到 InitialViewController
.
希望对您有所帮助!