objective c - Changing label's text in another view controller -


i have 1 view controller named firstviewcontroller, , second named secondviewcontroller. present second view controller

 uiviewcontroller *controller = [self.storyboard instantiateviewcontrollerwithidentifier:@"maincontroller"]; [self presentviewcontroller:controller animated:yes completion:nil]; 

in secondviewcontroller's .m, want change text of uilabel in firstviewcontroller. however, label's text isn't updating. how make firstviewcontroller's label updated when uibutton pressed in secondviewcontroller?

you use delegate pattern

first create delegate protocol

@class secondviewcontroller;  @protocol secondviewcontrollerdelegate  -(void) updatelabelwithstring:(nsstring*)string  @end  @property (weak, nonatomic) id<secondviewcontrollerdelegate>delegate; 

in ibaction connected uibutton

[self.delegate updatelabelwithstring:yourstring]; 

in firstviewcontroller.h

#import "secondviewcontroller.h"  @interface firstviewcontroller : uiviewcontroller <secondviewcontrollerdelegate> 

in firstviewcontroller.m

-(void) updatelabelwithstring:(nsstring*)string {    label.text = string; }  

then when create controller instance, set firstviewcontroller delegate mainviewcontroller

controller.delegate = self; 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -