demuro1
demuro1

Reputation: 299

compare content of UIButton Selected state

I am learning objective-c for iOS programming. I am making an app that has 2 round rect buttons and a label. I want to compare the values of the content of the buttons and output text to the label based on whether the buttons match or not. I have not been able to figure out how to compare the content of the buttons though. I have a model file and the view controller that I'll post below.

Thanks for any help you can provide

This is model.h file

//  MatchTest.h
#import <Foundation/Foundation.h>

@interface MatchTest : NSObject

-(NSString*)doesItMatch:(UIButton *)sender;
-(NSString*)doesItMatchGroup:(NSArray *)buttonGroup;

@end

This is 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: Do Not Match";
            NSLog(@"%@",buttonGroup[i]);
        }

    }

    return tempString;
}
@end

This is ViewController.h file

//  MatchViewController.h
#import <UIKit/UIKit.h>
#import "MatchTest.h"
@interface MatchViewController : UIViewController
@end

This is 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

Upvotes: 2

Views: 511

Answers (2)

demuro1
demuro1

Reputation: 299

ok I figured out exactly how it needs to work @rmaddy I really appreciate your help. your suggestions really helped me finish this! Here is my solution:

Match.h

//  MatchTest.h
#import <Foundation/Foundation.h>
@interface MatchTest : NSObject
-(NSString*)doesItMatch:(UIButton *)sender;//kept for legacy purposes
-(NSString*)doButtonsMatch:(NSArray *)buttonGroup;
@end

Match.m

//  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*)doButtonsMatch:(NSArray *)buttonGroup
{
    NSString *returnString;

    NSString *tempString;
    NSString *otherString;

    int count = 0;

    for (UIButton *button in buttonGroup)
    {
        if(button.isSelected)
        {
            tempString = [button titleForState:UIControlStateSelected];
        }
        else {tempString = [button titleForState:UIControlStateNormal];}

        for(UIButton *otherButton in buttonGroup)
        {
        count++;

        if(otherButton.isSelected)
        {
           otherString = [otherButton titleForState:UIControlStateSelected];
        }
        else {otherString = [otherButton titleForState:UIControlStateNormal];}

        NSLog(@"%i - %@ - %@",count,tempString,otherString);

        if (![tempString isEqual:otherString])
        {
            returnString = @"Button: Unmatched";
            break;
        }
        else{returnString = @"Button: Matched";}
    }

    if (![tempString isEqual:otherString]){break;}
}

return returnString;
}
@end

and then the MatchViewController.m

//  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; //flips the button
    self.matchLabel.text = [self.match doButtonsMatch:self.buttonGroup];
}

@end

Upvotes: 1

rmaddy
rmaddy

Reputation: 318944

Assuming your buttonGroup array contains UIButton objects, then your code for doesItMatchGroup needs to be updated to properly compare the titles of the buttons. At least I believe that is your intended goal. You are unclear by what you mean by "compare the values of the content of the buttons".

Assuming you want to see if the button titles match then you do this:

- (NSString *)doesItMatchGroup:(NSArray *)buttonGroup {
    NSString* tempString = @"Buttons: Match";

    for (int i = 1; i < 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: Do Not Match";
            NSLog(@"%@",buttonGroup[i]);
            break; // no need to check any more buttons
        }
    }

    return tempString;
}

Upvotes: 1

Related Questions