比较UIButton Selected状态的内容

时间:2013-08-19 22:26:28

标签: ios uibutton

我正在学习iOS编程的objective-c。我正在制作一个有2个圆形矩形按钮和标签的应用程序。我想根据按钮是否匹配,将按钮内容和输出文本的值与标签进行比较。我无法弄清楚如何比较按钮的内容。我有一个模型文件和我将在下面发布的视图控制器。

感谢您提供的任何帮助

这是model.h文件

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

@interface MatchTest : NSObject

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

@end

这是model.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*)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

这是ViewController.h文件

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

这是ViewController.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;
    self.matchLabel.text = [self.match doesItMatchGroup:self.buttonGroup];
}

@end

2 个答案:

答案 0 :(得分:1)

假设您的buttonGroup数组包含UIButton个对象,则需要更新doesItMatchGroup的代码以正确比较按钮的标题。至少我相信这是你的预期目标。您不清楚“比较按钮内容的值”的含义。

假设您想查看按钮标题是否匹配,则执行以下操作:

- (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;
}

答案 1 :(得分:1)

好的,我确切地知道它需要如何工作@rmaddy我非常感谢你的帮助。你的建议真的帮助我完成了这个!这是我的解决方案:

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

然后是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