iphone - Accessing variables from another ViewController -
firstviewcontroller.h:
#import <uikit/uikit.h> @interface firstviewcontroller : uiviewcontroller { nsarray *listdata; } -(ibaction) gotoinsert: (id) sender; @property (nonatomic, retain) nsarray *listdata; @end
firstviewcontroller.m:
-(ibaction) upisirezultat:(id)sender { secondviewcontroller *secondview = [[secondviewcontroller alloc] initwithnibname: nil bundle: nil]; [self presentmodalviewcontroller: secondview animated: no]; [secondview release]; } - (void)viewdidload {nsarray *array = [[nsarray alloc] initwithobjects:@"236", @"46", @"147", @"8", @"56", @"69", @"114", @"2", @"96", @"518", @"2", @"54", @"236", nil]; self.listdata = array; [array release]; [super viewdidload]; }
secondviewontroller.h
@interface secondviewcontroller : uiviewcontroller { } -(ibaction) insert; @end
secondviewontroller.m
-(ibaction) insert { /* here should code insert number in listdata firstviewcontroller */ }
so when app loads loads firstviewcontroller.xib , shows listdata array on screen, when click button "go insert" view loaded (secondviewcontroller.xib) button "insert" should add number array , display new array on first view.
how that?
you can access parent view controller self.parentviewcontroller
. therefore along these lines (meaning haven't tested -- should) should work:
firstviewcontroller *firstviewcontroller = self.parentviewcontroller; nsmutablearray *newarray = [[nsmutablearray alloc] initwitharray:firstviewcontrol.listdata]; [newarray addobject:@"42"]; [firstviewcontroller setlistdata:[nsarray arraywitharray:newarray]]; [newarray release];
however, since want add objects listarray
, more natural use nsmutablearray
instead. also, adding nsstring
s array, when looks more want have nsnumber
objects.
Comments
Post a Comment