objective c - iOS: Global property variable not being set even when value is passed to it -
i have set of code parses xml file find user id number. id number sent view controller soap wsdl request. problem value never set.
my 2 classes loginpageviewcontroller , emimenutableviewcontroller.
i trying pass integer value loginpageviewcontroller emimenutableviewcontroller.
here header file emimenutableviewcontroller:
#import <uikit/uikit.h> @interface emimenutableviewcontroller : uitableviewcontroller @property (nonatomic) int usernumber; @end
here header file loginpageviewcontroller:
#import <uikit/uikit.h> @interface loginpageviewcontroller : uiviewcontroller <nsxmlparserdelegate> { nsxmlparser *xmlparser; nsstring *classelement; nsmutablearray *titarry; nsmutablearray *linkarray; bool itemselected; nsmutablestring *mutttitle; nsmutablestring *mutstrlink; int usernumber; nsstring *parsedstring; } // username , password text fields @property (weak, nonatomic) iboutlet uitextfield *usernametextfield; @property (weak, nonatomic) iboutlet uitextfield *passwordtextfield; // logging in , registering - (ibaction)loginbuttonselected:(id)sender; @property (weak, nonatomic) iboutlet uibutton *registerbutton; // unwind segue ibaction logging out - (ibaction)logoutandunwind:(uistoryboardsegue *)segue; @end
here value set in loginpageviewcontroller:
- (void)parserdidenddocument:(nonnull nsxmlparser *)parser { emimenutableviewcontroller *controller = [[emimenutableviewcontroller alloc] init]; // if user authenticated, allow them proceed segue if([parsedstring containsstring:@"user authenticated"]) { >>>controller.usernumber = usernumber<<<; - set here nslog(@"user:%d", usernumber); [self performseguewithidentifier:@"login_success" sender:self]; nslog(@"logged in"); // if server says user isn't found, notify user create account. } else if([parsedstring containsstring:@"user not found"]) { uialertview *alert = [[uialertview alloc] initwithtitle:@"user not found" message:@"please create account!" delegate:self cancelbuttontitle:@"close" otherbuttontitles:nil, nil]; [alert show]; // if server replies password incorrect, notify user. } else if([parsedstring containsstring:@"password incorrect"]) { uialertview *alert = [[uialertview alloc] initwithtitle:@"incorrect username/password" message:@"plese try again." delegate:self cancelbuttontitle:@"close" otherbuttontitles:nil, nil]; [alert show]; } else { uialertview *alert = [[uialertview alloc] initwithtitle:@"an error has occurred!" message:@"please check network connection , try again." delegate:self cancelbuttontitle:@"close" otherbuttontitles:nil, nil]; [alert show]; } }
here code shows value isn't being set (this in emimenutableviewcontroller):
nslog(@"user: %d", _usernumber);
also, here console output. can see value set in loginpageviewcontroller logged it, when logged in emimenutableviewcontroller, 0.
2015-07-08 15:09:41.151 emi[1971:173596] user:27808 2015-07-08 15:09:41.179 emi[1971:173596] logged in 2015-07-08 15:09:41.186 emi[1971:173596] user: 0
i figured doing right, can't figure out wrong. setting variable wrong?
thanks.
this line...
emimenutableviewcontroller *controller = [[emimenutableviewcontroller alloc] init];
creates emimenutable vc, , line
controller.usernumber = usernumber;
sets property on it. controller lives milliseconds. released arc when goes out of scope.
meanwhile, performsegue
creates new copy of emimenutablevc, , 1 never gets chance hear usernumber
found in parse.
so need value parse conveyed segue's destination view controller. simple way of doing declare usernumber property of loginvc also, e.g.
@interface loginpageviewcontroller : uiviewcontroller <nsxmlparserdelegate> // ... // more formal emimenutable vc, equivalent... @property (nonatomic, assign) nsinteger usernumber;
with that, parser shouldn't build own emimenutable vc. set property on self...
if([parsedstring containsstring:@"user authenticated"]) { // ... self.usernumber = usernumber; // ... [self performseguewithidentifier:@"login_success" sender:self];
now, implement prepare segue, , set value on destination vc.
edit - changed understanding segue points navigation view controller...
// sure import #import "emimenutableviewcontroller.h" - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"login_success"]) { uinavigationcontroller *navvc = (uinavigationcontroller *)segue.destinationviewcontroller; emimenutableviewcontroller *vc = (emimenutableviewcontroller *)navvc.viewcontrollers[0]; // vc.usernumber = self.usernumber; } }
a funkier approach misuse sender:
parameter of performsegue
. can skip instance variable loginvc, , pass value directly this...
[self performseguewithidentifier:@"login_success" sender:@(usernumber)];
this weird, because performsegue sender number. work, since type generic id
. prepare method looks same, except...
if ([segue.identifier isequaltostring:@"login_success"]) { uinavigationcontroller *navvc = (uinavigationcontroller *)segue.destinationviewcontroller; emimenutableviewcontroller *vc = (emimenutableviewcontroller *)navvc.viewcontrollers[0]; nsnumber *paramtrick = (nsnumber *)sender; vc.usernumber = [paramtrick intvalue]; }
but guess don't recommend way, if you're starting out.
Comments
Post a Comment