objective c - How to move methods out of app delegate? -


i trying move methods out of app delegate , instead using separate view controller conform protocol. old 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 question view controller question , push onto 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 gets called every time question answered. loads next question , pushes onto nav stack.  pushes pointer question , result linked-list called firstanswer->nextanswer->nextanswer, etc.   when next question returns nil know we've finished there no more questions , log linked-list 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 questionnaire"                                                            delegate:nil                                                   cancelbuttontitle:@"ok"                                                   otherbuttontitles:nil];         [alertview show];         [self.qnaire loganswers];     } } 

and i'm trying move these methods initialviewcontroller , button nothing ?

@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];  } 

thanks in advance.

initialviewcontroller button should call vrquestionviewcontroller (given below)

@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];     // additional setup after loading 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 resources can 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 

my current 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; } 

and 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 question view controller question , push onto 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 gets called every time question answered. loads next question , pushes onto nav stack.  pushes pointer question , result linked-list called firstanswer->nextanswer->nextanswer, etc.   when next question returns nil know we've finished there no more questions , log linked-list 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 questionnaire"                                                            delegate:nil                                                   cancelbuttontitle:@"ok"                                                   otherbuttontitles:nil];         [alertview show];         [self.qnaire loganswers];     } }    - (void)viewdidload {     [super viewdidload];      // additional setup after loading view nib. }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  /* #pragma mark - navigation  // in storyboard-based application, want little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {     // new view controller using [segue destinationviewcontroller].     // pass selected object new view controller. } */  @end 

have ctrl-drag nav controller make initialviewcontroller root view controller. ctrl-dragged button vrquestionviewcontroller.

in simulator when press button vrquestionviewcontroller shown no question or response y/n

sorry making real pigs ear if this!

the code in question seems example code on github listed @ https://github.com/rwarrender/dynamicquestions

for sake of readability quite bit of logic happens in app delegate. app delegate initialises vrquestionnaire instance , sets questions. sets first question view controller on navigation controller. each vrquestionviewcontroller configured app delegate vrquestionviewcontrollers delegate too. once question has been answered, calls questionviewcontroller:didanswerquestion:withresult: app delegate pushs result onto questionnaire's answer list , moves along question tree display next question.

as wain has suggested if wanted expand more example move app delete code controller class. you're having trouble initial view controller class because wrapped inside navigation controller, therefore navigation controller should initial view controller in storyboard initialviewcontroller class root view controller of navigation controller. right click dragging navigation controller in storyboard initialviewcontroller.

hope helps!


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -