ios - Set button title programatically -
i made button in storyboard , associated ibaction in header file. how can set title of button variable made displayphone
, have call number well?
.h
#import <uikit/uikit.h> #import <parse/parse.h> @interface ibthirdviewcontroller : uiviewcontroller @property (nonatomic, strong) pfrelation *agentrelation; @property (nonatomic, strong) nsarray *agent; @property (weak, nonatomic) iboutlet uilabel *agentname; @property (strong, nonatomic) iboutlet uilabel *agentphone; @property (strong, nonatomic) iboutlet uilabel *agentemail; @property (strong, nonatomic) iboutlet uiimageview *agentimage; - (ibaction)phonebutton:(id)sender; @end
.m
#import "ibthirdviewcontroller.h" #import "ibagentstableviewcontroller.h" @interface ibthirdviewcontroller () @end @implementation ibthirdviewcontroller - (void)viewdidload { [super viewdidload]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; //find agent , show self.agentrelation = [[pfuser currentuser] objectforkey:@"agentrelation"]; pfquery *query = [self.agentrelation query]; [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) { if (!error) { // success self.agent = objects; // found objects (pfobject *object in objects) { nslog(@"name: %@", [object objectforkey:@"name"]); nslog(@"email: %@", [object objectforkey:@"email"]); nsstring *displayemail = [object objectforkey:@"email"]; nsstring *displayname = [object objectforkey:@"name"]; nsstring *displayphone = [object objectforkey:@"phone"]; pffile *thumbnail = [object objectforkey:@"profilepic"]; nsurl *imagefileurl = [[nsurl alloc] initwithstring:thumbnail.url]; nsdata *imagedata = [nsdata datawithcontentsofurl:imagefileurl]; self.agentemail.text = displayemail; self.agentname.text = displayname; self.agentphone.text = displayphone; self.agentimage.image = [uiimage imagewithdata:imagedata]; } } else { // log details of failure nslog(@"error: %@ %@", error, [error userinfo]); } }]; } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"showeditagent"]) { ibagentstableviewcontroller *viewcontroller = (ibagentstableviewcontroller *)segue.destinationviewcontroller; viewcontroller.agents = [nsmutablearray arraywitharray:self.agent]; } } - (ibaction)phonebutton:(id)sender { } @end
first of need connect button xib iboutlet
object:
@property (nonatomic, weak) iboutlet uibutton *displayphone;
then set it's title in -(void)viewdidappear:(bool)animated
after loading data:
- (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; //... // load data here //... [self.displayphone settitle:displayphone forstate:uicontrolstatenormal]; }
finally implement ibaction
method button:
- (ibaction)phonebutton:(id)sender { nsstring *phone = [sender titleforstate:uicontrolstatenormal]; // remove chars except of digits static nsstring *const kdigitsstring = @"0123456789"; phone = [[phone componentsseparatedbycharactersinset:[[nscharacterset charactersetwithcharactersinstring:kdigitsstring] invertedset]] componentsjoinedbystring:@""]; // initiate call [[uiapplication sharedapplication] openurl:[nsurl urlwithstring:[nsstring stringwithformat:@"tel:%@", phone]]]; }
Comments
Post a Comment