objective c - Help with setTitle on iOS -
i rookie programming. have entered in following code , app runs without bugs crashes when button pressed. goal identify if button pressed once or twice. if pressed third time, should reset never being pressed.
buttontestviewcontroller.h
#import <uikit/uikit.h> @interface buttontestviewcontroller : uiviewcontroller { } -(ibaction)pressbutton:(id)sender; @end
buttontestviewcontroller.m
@implementation buttontestviewcontroller -(ibaction)pressbutton:(id)sender{ static int counter; if (counter == 0) { [sender settitle:@"not answered"]; }else if (counter == 1) { [sender settitle:@"pressed once"]; }else if (counter == 2) { [sender settitle:@"pressed twice"]; } counter += 1; if (counter > 2) { counter = 0; } } - (void)dealloc { [super dealloc]; } @end
i change background color of button when pressed , continue errors if use setbackgroundcolor. thank in advance time , consideration.
you need initialize counter
. number in legal range of int
s how have right now, , changes every time method called.
static int counter = 0;
also move line outside of method declaration counter
isn't reset 0 every time method called. or use instance variable instead of static one. that's should need working.
what error when try use setbackgroundcolor:
?
edit
also, if control uibutton
, settitle:
isn't valid method. check api docs apple. you'd need like:
[[sender titlelabel] settext:@"not answered"];
Comments
Post a Comment