ios - Using a Class to save a string in viewcontroller A and Logging in viewController B -
i'm trying larn how classes work , i've nail rut. string saving place class in viewcontroller a, it's accessing null in viewconrollerb. can explain why happening? said i'm unfamiliar classes , trying learn.
code view controller (when click on button name associated button saved)
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { } self.place=[[place alloc]init]; homecoming self; } - (void) buttonpressed:(uibutton *) sender { nslog(@"works"); self.place.name=self.name; nslog(@"%@",self.place.name); } the class.m
@implementation place -(id)init { self=[super init]; homecoming self; } @end viewcontroller b:
- (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { self.title=@"shack"; } self.place=[[place alloc]init]; homecoming self; } - (void)viewwillappear:(bool)animated { nslog(@"%@",self.place.name); } the class.h
@interface place : nsobject @property (strong,nonatomic) nsstring *name; @end viewcontrollerb.h
#import "place.h" @interface favoritestableviewcontroller : uitableviewcontroller @property (nonatomic) nsstring * name; @property (strong,nonatomic) place * place; @end viewcontrollera.h
@interface moreviewcontroller : uiviewcontroller @property (nonatomic,strong) nsstring * name; @property (nonatomic,strong) place *place; @end
in viewcontrollerb, create new place in initwithstyle. place different place created in viewcontrollera. therefore, when access name property, access null. prepare this, need set place on 2nd vc on 1st one. create pointer allows access original name.
ios objective-c class uiviewcontroller
No comments:
Post a Comment