ios - compare content of UIButton Selected state -
i learning objective-c ios programming. making app has 2 round rect buttons , label. want compare values of content of buttons , output text label based on whether buttons match or not. have not been able figure out how compare content of buttons though. have model file , view controller i'll post below.
thanks can provide
this model.h file
// matchtest.h #import <foundation/foundation.h> @interface matchtest : nsobject -(nsstring*)doesitmatch:(uibutton *)sender; -(nsstring*)doesitmatchgroup:(nsarray *)buttongroup; @end
this model.m file
// matchtest.m #import "matchtest.h" @implementation matchtest -(nsstring*)doesitmatch:(uibutton *)sender { nsstring* tempstring; if(sender.isselected) { tempstring = @"selected"; } else { tempstring = @"not selected"; } return tempstring; } -(nsstring*)doesitmatchgroup:(nsarray *)buttongroup { nsstring* tempstring = @"buttons: match";; for(int i=1;i<buttongroup.count;i++) { if(buttongroup[i-1] != buttongroup[i]) { tempstring = @"buttons: not match"; nslog(@"%@",buttongroup[i]); } } return tempstring; } @end
this viewcontroller.h file
// matchviewcontroller.h #import <uikit/uikit.h> #import "matchtest.h" @interface matchviewcontroller : uiviewcontroller @end
this viewcontroller.m file
// matchviewcontroller.m #import "matchviewcontroller.h" @interface matchviewcontroller () @property (weak, nonatomic) iboutlet uilabel *matchlabel; @property (strong, nonatomic) matchtest *match; @property (strong, nonatomic) iboutletcollection(uibutton) nsarray *buttongroup; @end @implementation matchviewcontroller -(matchtest *)match { if(!_match) _match = [[matchtest alloc] init]; return _match; } -(nsarray *)buttongroup { if(!_buttongroup) _buttongroup = [[nsarray alloc] init]; return _buttongroup; } - (ibaction)button:(uibutton *)sender { sender.selected = !sender.isselected; self.matchlabel.text = [self.match doesitmatchgroup:self.buttongroup]; } @end
assuming buttongroup
array contains uibutton
objects, code doesitmatchgroup
needs updated compare titles of buttons. @ least believe intended goal. unclear mean "compare values of content of buttons".
assuming want see if button titles match this:
- (nsstring *)doesitmatchgroup:(nsarray *)buttongroup { nsstring* tempstring = @"buttons: match"; (int = 1; < buttongroup.count; i++) { uibutton *btn1 = buttongroup[i - 1]; uibutton *btn2 = buttongroup[i]; nsstring *title1 = [btn1 titleforstate:uicontrolstatenormal]; nsstring *title2 = [btn2 titleforstate:uicontrolstatenormal]; if (![title1 isequaltostring:title2]) { tempstring = @"buttons: not match"; nslog(@"%@",buttongroup[i]); break; // no need check more buttons } } return tempstring; }
Comments
Post a Comment