ios - ivar for view controller in appdelegate.m -


edit 1

i adding code indicate state after have tried work codeinorange's answer far far behaves code behaved, sample link shows @ first in text field , can altered user, when user returns vc, new link text has been replaced original sample link. reason posting additional code try reconnect codeinorange's promising answer because misunderstanding logical flow of original suggestions , later comments.

in current storyboard leaving text field , placeholder text empty because sample link seems adequately supplied viewdidload method below.

- (void)viewdidload {     [super viewdidload];     self.urlnameinput.text = @"sample http";     // additional setup after loading view, typically nib.     self.urlnameinput.clearbuttonmode = uitextfieldviewmodealways;     self.urlnameinput.clearsonbeginediting = no;    }   - (bool)textfieldshouldreturn:(uitextfield *)textfield {      if (textfield == self.urlnameinput) {         [textfield resignfirstresponder];         [self processpbn];     }     return yes; }   - (void)viewdidappear:(bool)animated {     appdelegate *appdelegate = [uiapplication sharedapplication].delegate;      // self.urlnameinput.text = appdelegate.stringfortextfield;     appdelegate.stringfortextfield = self.urlnameinput.text; }  - (void) processpbn {     nsurlrequest *therequest = [nsurlrequest requestwithurl:[nsurl urlwithstring:self.urlnameinput.text] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0];      [nsurlconnection sendasynchronousrequest:therequest queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *connection, nsdata *data, nserror *error)      {          // lots of detail code has been elided in method           self.iboard = 0;          nsregularexpression *regex = [nsregularexpression  regularexpressionwithpattern:tomatch options:nsregularexpressiondotmatcheslineseparators error:&error];          (nstextcheckingresult* board in [regex matchesinstring:string options:nsregularexpressiondotmatcheslineseparators range:nsmakerange(0, [string length])])          {           if (self.iboard>0) {              appdelegate *appdelegate = [uiapplication sharedapplication].delegate;               appdelegate.stringfortextfield = self.urlnameinput.text;          }      }]; } 

edit 1

edit 0

i not want preserve text between application shutdowns , launches, answer using nsuserdefaults not quite need.

also, appears trials solution suggested michael dautermann suggests either putting intialization text in viewdidload or in xib or storyboard, not work because text returns initial value upon return vc (likely because viewdidload method triggered), think need create ivar in appdelegate.m asked in original question, , not in viewcontroller.m viewdidload, to desired result, apparently. perhaps easier create b00l ivar in appdelegate.m flag tells whether original text or current text desired. cannot figure out how that, either. so, please consider edit in answer.

edit 0

my appdelegate.m contains following code.

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {     self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];         uistoryboard *sb = [uistoryboard storyboardwithname:@"mainstoryboard" bundle:nil];         bdviewcontroller *vc = [sb instantiateinitialviewcontroller];         self.viewcontroller = (id)vc;     }     self.window.rootviewcontroller = self.viewcontroller;      [self.window makekeyandvisible];      return yes; } 

in vc want ivar, nsstring, set @ launch can example text in uitextfield. later want uitextfield adjusted new value when user supplies valid text uitextfield.

currently in vc.h, text field declared , synthesized in vc.m follows .

@property (nonatomic, strong)  uitextfield *urlnameinput;   @synthesize urlnameinput; 

i have tried putting following code didfinishlaunchingwithoptions: not see desired text when run app.

    self.viewcontroller.urlnameinput.text = @"example http"; 

how can programmatically accomplish goal of initializing uitextfield?

put "urlnameinput.text =" bit view controller's "viewdidload" method, instead of "didfinishlaunchingwithoptions:" method (where view controller not yet instantiated.

even better that, set initial text in storyboard or xib file , can programmatically adjust later on.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -