2

我正在学习 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
4

2 回答 2

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;
}
于 2013-08-19T22:41:05.863 回答
1

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

匹配.h

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

比赛.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
于 2013-08-20T09:04:07.747 回答