Friday, 15 April 2011

ios - Cannot set the value of a NSString from another class -



ios - Cannot set the value of a NSString from another class -

i've searched on google, , stack overflow solution, wasn't able find reply solved problem. sorry long post, i'm trying give much info can. i'm new ios , objective- c, not programming in general due beingness asked switch on android company, help appreciated.

i'm trying assign value nsstring in 1 class textfield in another, error: **-[viewcontroller name:]: unrecognized selector sent instance 0x78712fe0** when run app in simulator.

relevant code:

//userinfo.h #import <foundation/foundation.h> @interface userinfo : nsobject <nscoding> @property (nonatomic, strong) nsstring *name; @property (nonatomic, strong) nsstring *age; @property (nonatomic, strong) nsstring *address; @end //userinfo.m #import "userinfo.h" static nsstring *namekey = @"username"; static nsstring *agekey = @"userage"; static nsstring *addresskey = @"useraddress"; static nsstring *userinfokey = @"userinfokey"; @implementation userinfo @synthesize name; @synthesize age; @synthesize address; - (id) initwithcoder:(nscoder *)coder { self = [super init]; self.name = [coder decodeobjectforkey:namekey]; self.age = [coder decodeobjectforkey:agekey]; self.address = [coder decodeobjectforkey:addresskey]; homecoming self; } - (void)encodewithcoder:(nscoder *)coder { [coder encodeobject:self.name forkey:namekey]; [coder encodeobject:self.age forkey:agekey]; [coder encodeobject:self.address forkey:addresskey]; } @end //viewcontroller.h #import <uikit/uikit.h> #import "userinfo.h" @interface viewcontroller : uiviewcontroller <uitextfielddelegate, uitextviewdelegate, nscoding> @property (nonatomic, strong) userinfo *userinfoobject; @property (nonatomic, weak) iboutlet uilabel *titlelabel; @property (nonatomic, weak) iboutlet uilabel *namelabel; @property (nonatomic, weak) iboutlet uilabel *agelabel; @property (nonatomic, weak) iboutlet uilabel *addresslabel; @property (nonatomic, weak) iboutlet uitextfield *nametext; @property (nonatomic, weak) iboutlet uitextfield *agetext; @property (nonatomic, weak) iboutlet uitextfield *addresstext; @property (nonatomic, weak) iboutlet uibutton *savebtn; - (ibaction)savebtntouched:(id)sender; - (void) saveuserinfo; - (void) loaduserinfo; - (void) setuserinterfacevalues; - (ibaction)nametext:(id)sender; - (ibaction)agetext:(id)sender; - (ibaction)addresstext:(id)sender; @end //viewcontroller.m #import "viewcontroller.h" #import "userinfo.h" @interface viewcontroller () @end @implementation viewcontroller @synthesize titlelabel; @synthesize namelabel; @synthesize agelabel; @synthesize addresslabel; @synthesize nametext; @synthesize agetext; @synthesize addresstext; @synthesize savebtn; static nsstring *userinfokey = @"userinfokey"; - (void)viewdidload { [super viewdidload]; [self loaduserinfo]; if(!self.userinfoobject) { self.userinfoobject = [[userinfo alloc] init]; } [self setuserinterfacevalues]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } - (bool)textfieldshouldbeginediting:(uitextfield *)textfield { self.savebtn.enabled = yes; homecoming yes; } - (bool)textfieldshouldreturn:(uitextfield*)textfield { homecoming yes; } - (bool) textfieldshouldendediting:(uitextfield *)textfield { [self.nametext resignfirstresponder]; [self.agetext resignfirstresponder]; [self.addresstext resignfirstresponder]; homecoming yes; } - (ibaction)savebtntouched:(id)sender { nslog(@"%@ entered name field", self.nametext.text); nslog(@"%@ entered age field", self.agetext.text); nslog(@"%@ entered address field", self.addresstext.text); [self textfieldshouldendediting:self.nametext]; [self textfieldshouldendediting:self.agetext]; [self textfieldshouldendediting:self.addresstext]; self.userinfoobject.name = self.nametext.text; self.userinfoobject.age = self.agetext.text; self.userinfoobject.address = self.addresstext.text; [self saveuserinfo]; self.savebtn.enabled = no; } - (void)saveuserinfo { nsdata *userinfodata = [nskeyedarchiver archiveddatawithrootobject:self.userinfoobject]; [[nsuserdefaults standarduserdefaults] setobject:userinfodata forkey:userinfokey]; } - (void)loaduserinfo { nsdata *userinfodata = [[nsuserdefaults standarduserdefaults] objectforkey:userinfokey]; if(userinfodata) { self.userinfoobject = [nskeyedunarchiver unarchiveobjectwithdata:userinfodata]; } } - (void) setuserinterfacevalues { self.nametext.text = self.userinfoobject.name; self.agetext.text = self.userinfoobject.age; self.addresstext.text = self.userinfoobject.address; } - (ibaction)nametext:(id)sender { } - (ibaction)agetext:(id)sender { } - (ibaction)addresstext:(id)sender { } @end //appdelegate.h #import <uikit/uikit.h> #import "viewcontroller.h" #import "userinfo.h" @interface appdelegate : uiresponder <uiapplicationdelegate> @property (strong, nonatomic) uiwindow *window; @property (strong, nonatomic) viewcontroller *viewcontroller; @end //appdelegate.m #import "appdelegate.h" #import "viewcontroller.h" #import "userinfo.h" @interface appdelegate () @end @implementation appdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // override point customization after application launch. self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; self.viewcontroller = [[viewcontroller alloc] initwithnibname:@"viewcontroller" bundle:nil]; self.window.rootviewcontroller = self.viewcontroller; [self.window makekeyandvisible]; homecoming yes; } //all other generated methods. taken out due space. @end

three breakpoints set supposidly source of problem:

from viewcontroller.m, in - (void)setuserinterfacevalues

self.nametext.text = self.userinfoobject.name; (i assume applies other 2 lines below also)

also viewcontroller.m, in - (void)viewdidload [self setuserinterfacevalues];

and appdelegate.m, in - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions

self.window.rootviewcontroller = self.viewcontroller;

from understand, , have learned searching issue, app trying send info doesn't exist, nsstring name beingness culprit. others have suggested create sure .xib file connected viewcontroller, , have verified is.

as bit of information, i'm not using storyboard app, , instead using interface builder. i'm aware there advantages storyboards, , using them, company uses interface builder , lot of things programmatically, i'm learning develop without.

[edit]: issue solved ian.

ios objective-c

No comments:

Post a Comment