Saturday, 15 January 2011

ios - Calling Method From Another UIViewController Has No Visible Effect -



ios - Calling Method From Another UIViewController Has No Visible Effect -

i have 2 classes , want phone call method 1 class when button pressed. declare in .h file so:

-(void) imagechange;

and created method in .m this:

-(void)imagechange { uiimage *image = [uiimage imagenamed: img]; [_myimage setimage:image]; }

finally, tried phone call method class using:

- (ibaction)done:(id)sender { secondviewcontroller *theinstance = [[secondviewcontroller alloc] init]; [theinstance imagechange]; [self dismissviewcontrolleranimated:yes completion:nil]; }

however, when press "done" in view controller, uiimage doesn't change. please note: img nsstring value.

the question variation on mutual one: "how pass values between view controllers", , code represents mutual effort @ solution. let's start did.

your app has 2 view controllers views on view stack, , want communicate between them. line:

secondviewcontroller *theinstance = [[secondviewcontroller alloc] init];

creates brand new instance of secondviewcontroller (alloc means allocate memory new instance of class). line:

[theinstance imagechange];

communicates it, in case looks setting image view's image. line:

}

implicitly destroys new instance, since it's never referred again. code succeeds in communicating secondviewcontroller, wrong instance, instance lives few milliseconds.

okay, it? @rmaddy saying go find existing instance of secondviewcontroller, , communicate that. how ahold of existing instance depends on how got here. dismissviewcontrolleranimated in code makes me think current vc presented instance of secondvc. if so,

(secondviewcontroller *)self.presentingviewcontroller

points need. if in uinavigationcontroller, dig through it's viewcontrollers stack, here:

nsarray *stack = self.navigationcontroller.viewcontrollers; secondviewcontroller *secondvc = stack[stack.count-2];

but, while might straightest line b, it's not design because makes current view controller dependent in brittle way on how got presented.

hence @crimsonchris makes suggestion consider delegation. peoples' go-to pattern vc needs communicate another. there plenty of web , resources on how this, won't repeat here. check out this, example, or google "ios delegation".

there other ways, nsnotificationcenter broadcast that's interested whatever want communicate, or kvo lets the secondvc observe alter in model , react, no matter how or why alter made.

the key concept these latter 2 app needs have model, set of objects describes app state. view controllers not model -- in fact they're exactly not model. job larn model changes , modify views accordingly.

for case, code posted shouldn't trying image set in other view controller, should recording model whatever user action happened triggers dismissal. when current vc dismisses itself, secondviewcontroller (assuming did present) viewwillappear. method might place check model status set on user action. secondviewcontroller can phone call imagechange on itself.

hope clear enough. luck.

ios objective-c cocoa-touch design uiviewcontroller

No comments:

Post a Comment