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