iOS can't set backgroundImage of UIButton -
i can't seem make system use custom created button have. have following declarations in interface section of .m file:
@property (weak, nonatomic) iboutlet uibutton *uselocationbutton; @property (weak, nonatomic) iboutlet uibutton *useaddressbutton; then have following init function
- (id)init { if (self = [super initwithnibname:@"tcmdirectionsviewcontroller" bundle:nil]){ [[self navigationitem] settitle:@"get directions"]; uibarbuttonitem *cancelitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel target:self action:@selector(cancel:)]; click = no; [[self navigationitem] setleftbarbuttonitem:cancelitem]; uiimage *buttonimage = [[uiimage imagenamed:@"bluebutton.png"] resizableimagewithcapinsets:uiedgeinsetsmake(18, 18, 18, 18)]; uiimage *buttonimagehighlight = [[uiimage imagenamed:@"bluebuttonhighlight.png"] resizableimagewithcapinsets:uiedgeinsetsmake(18, 18, 18, 18)]; [[self uselocationbutton] setbackgroundimage:buttonimage forstate:uicontrolstatenormal]; [[self uselocationbutton] setbackgroundimage:buttonimagehighlight forstate:uicontrolstatehighlighted]; [[self useaddressbutton] setbackgroundimage:buttonimage forstate:uicontrolstatenormal]; [[self useaddressbutton] setbackgroundimage:buttonimagehighlight forstate:uicontrolstatehighlighted]; _locationmanager = [[cllocationmanager alloc] init]; [_locationmanager setdelegate:self]; [_locationmanager setdesiredaccuracy:kcllocationaccuracynearesttenmeters]; [_locationmanager startupdatinglocation]; } return self; } when run app though buttons default white buttons created in interface builder.
the init method called before view loaded, uselocationbutton doesn't exist yet.
put view setup in viewdidload method of uiviewcontroller this:
- (void)viewdidload { [super viewdidload]; uiimage *buttonimage = [[uiimage imagenamed:@"bluebutton.png"] resizableimagewithcapinsets:uiedgeinsetsmake(18, 18, 18, 18)]; uiimage *buttonimagehighlight = [[uiimage imagenamed:@"bluebuttonhighlight.png"] resizableimagewithcapinsets:uiedgeinsetsmake(18, 18, 18, 18)]; [[self uselocationbutton] setbackgroundimage:buttonimage forstate:uicontrolstatenormal]; [[self uselocationbutton] setbackgroundimage:buttonimagehighlight forstate:uicontrolstatehighlighted]; [[self useaddressbutton] setbackgroundimage:buttonimage forstate:uicontrolstatenormal]; [[self useaddressbutton] setbackgroundimage:buttonimagehighlight forstate:uicontrolstatehighlighted]; } also, make sure iboutlets connected. might want set breakpoint check if references valid.
Comments
Post a Comment