Changing IBOutlet UIObject in ViewController from another class using Objective C (iOS) -
i'm beginner in ios development , cannot part working. objective simple: have class named tcpcomm
connects server , sends data periodically. in storyboard have view containing buttons , textfield. idea change state of iboutlet
uibuttons
, uitextfield
based on received data server (from class).
i have tried using properties in different ways none of them worked.
any on best way please?
the best way achieve using delegates.
in tcpcomm header file declare protocol
@protocol tcpcommdelegate <nsobject> -(void)callbackmethod; @end
in interface declare public property
@property (nonatomic, weak) id<tcpcommdelegate> delegate;
now, call method declared in protocol whenever received data in tcpcomm class below
if(delegate && [delegate respondstoselector:@selector(callbackmethod)]) [delegate performselector:@selector(callbackmethod)];
now, in viewcontroller class make sure imported tcpcomm class , accepts tcpcommdelegate protocol below
@interface yourviewcontroller : uiviewcontroller <tcpcommdelegate>
in viewdidload method of viewcontroller create instance of tcpcomm class , assign delegate property self
tcpcomm *tcpcomm = [[tcpcomm alloc]init]; tcpcomm.delegate = self;
that's, can implement callbackmethod body in viewcontroller , change properties of whatever uiobjects want. method called whenever there new data fetched tcpcomm class, if have called 3rd code snippet @ right time i.e when data fetch completed
Comments
Post a Comment