gpt4 book ai didi

objective-c - 如何使用单例连接一组按钮?

转载 作者:行者123 更新时间:2023-12-01 19:23:18 26 4
gpt4 key购买 nike

我的 appDelegate,在方法 didFinishLaunchingWithOptions 中加载我的 View Controller ,然后创建一个单例:

UIViewController *MainWinVC = [[p3VC alloc] init];
[[self window] setRootViewController:MainWinVC];
ansButSingleton *ansButs = [[ansButSingleton alloc] init];

ansButSingleton.h 看起来像这样:
#import <Foundation/Foundation.h>

@interface ansButSingleton : NSObject {

// View objects:

UIButton *Ans1;
UIButton *Ans2;
UIButton *Ans3;
UIButton *Ans4;
UIButton *Ans5;
UIButton *Ans6;
UIButton *Ans7;
UIButton *Ans8;

// Other objects:

NSArray *ansButs;

}

@property (strong) UIButton *Ans1, *Ans2, *Ans3, *Ans4, *Ans5, *Ans6, *Ans7, *Ans8;
@property (strong) NSArray *ansButs;

+ (ansButSingleton *) ansButsName; // Declare class method
@end

和 ansButSingleton.m 像这样:
#import "ansButSingleton.h"

@implementation ansButSingleton

static ansButSingleton *ansButsName;

@synthesize Ans1, Ans2, Ans3, Ans4, Ans5, Ans6, Ans7, Ans8;
@synthesize ansButs;

//////////////////// instantiate ////////////////////

+ (ansButSingleton *) ansButsName { // class method

@synchronized(self)
{
if (!ansButsName)
ansButsName = [[ansButSingleton alloc] init];
return ansButsName;
}
}

//////////////////// initialize ////////////////////

- (id)init { // class instance method

NSLog(@"Initializing answer buttons");

if (ansButsName) {
return ansButsName;
}

self = [super init];

if(self) {

// First initialze the individual buttons

self.Ans1 = [[UIButton alloc] init];
[Ans1 setTitle:@"" forState:UIControlStateNormal];
self.Ans2 = [[UIButton alloc] init];
[Ans2 setTitle:@"" forState:UIControlStateNormal];
self.Ans3 = [[UIButton alloc] init];
[Ans3 setTitle:@"" forState:UIControlStateNormal];
self.Ans4 = [[UIButton alloc] init];
[Ans4 setTitle:@"" forState:UIControlStateNormal];
self.Ans5 = [[UIButton alloc] init];
[Ans5 setTitle:@"" forState:UIControlStateNormal];
self.Ans6 = [[UIButton alloc] init];
[Ans6 setTitle:@"" forState:UIControlStateNormal];
self.Ans7 = [[UIButton alloc] init];
[Ans7 setTitle:@"" forState:UIControlStateNormal];
self.Ans8 = [[UIButton alloc] init];
[Ans8 setTitle:@"" forState:UIControlStateNormal];

// Make an array containing the objects: this is the objective-C way!

self.ansButs = [[NSArray alloc] initWithObjects: Ans1, Ans2, Ans3, Ans4, Ans5, Ans6, Ans7, Ans8, nil];
}

NSLog(@"Done initializing answer buttons");

return self;
}
@end

这构建并运行良好(它还没有做太多)。由于成功加载 Nib ,按钮可见。但是,它们没有激活,因为我没有将它们连接到代码(没有 IBActions)。

问题:如何将这种方式创建的按钮连接到 Nib 中的按钮?如果这些是简单的按钮(不是按钮数组),我将创建一个方法并使用 IBAction 作为该方法声明的一部分。但这个案例似乎有点不同。或者可能不是。如果这些是标签(我稍后也需要这样做),我的阅读使我相信 IBOutletCollection 可能有效,但我看不到 IBActionCollection。需要专家指导!谢谢。

编辑...与 Rob 合作实现他的想法。我的 viewLoad 方法是从您的方法中复制和粘贴的,但其中可能有一些我需要更改的内容?
#pragma mark - View lifecycle

- (void)loadView {
NSDictionary *externals = [NSDictionary dictionaryWithObject:[AnswerButtons answerButtons]
forKey:@"answerButtons"];
NSDictionary *nibOptions = [NSDictionary dictionaryWithObject:externals
forKey:UINibExternalObjects];
[self.nibBundle loadNibNamed:self.nibName owner:self options:nibOptions];
[[AnswerButtons answerButtons] buttonsDidLoad];
}

最佳答案

有很多方法可以做到这一点。我会通过在 Nib 中连接单例的连接来做到这一点。就是这样。

首先,让我们修复单例类以匹配 iOS 编程约定,并为在 nib 中连接按钮连接提供支持:

答案按钮.h

#import <Foundation/Foundation.h>

@interface AnswerButtons : NSObject

@property (strong, nonatomic) IBOutlet UIButton *button1, *button2, *button3, *button4, *button5, *button6, *button7, *button8;
@property (strong, nonatomic, readonly) NSArray *buttons;

+ (AnswerButtons *)answerButtons;

- (void)buttonsDidLoad;

@end

AnswerButtons.m
#import "AnswerButtons.h"

@implementation AnswerButtons

@synthesize button1 = _button1;
@synthesize button2 = _button2;
@synthesize button3 = _button3;
@synthesize button4 = _button4;
@synthesize button5 = _button5;
@synthesize button6 = _button6;
@synthesize button7 = _button7;
@synthesize button8 = _button8;

@synthesize buttons = _buttons;

+ (AnswerButtons *)answerButtons {
static AnswerButtons *singleton;
static dispatch_once_t once;
dispatch_once(&once, ^{
singleton = [[AnswerButtons alloc] init];
});
return singleton;
}

- (void)buttonsDidLoad {
_buttons = [[NSArray alloc] initWithObjects:_button1, _button2, _button3, _button4, _button5, _button6, _button7, _button8, nil];
}

@end

请注意,我们创建 第一次请求单例时,类方法中的单例。不要在 application:didFinishLaunchingWithOptions: 中这样做.

接下来,让我们连接 Nib 中的按钮:
  • p3VC 打开 Nib 类(class)。 (您应该考虑将此名称更改为以大写字母开头并拼出 ViewController 或只是 Controller。)
  • 将“外部对象”从对象库中拖到 Nib 中。
  • 在 Identity Inspector 中,将外部对象的自定义类设置为 AnswerButtons .
  • 在 Attributes Inspector 中,将其 External Object Identifier 设置为 answerButtons .
  • 按住 Control 从 Answer Buttons 对象拖动到八个按钮中的每一个,将按钮连接到适当的 socket 。

  • 最后,我们需要提供 AnswerButtons当我们加载 nib 时,将单例添加到 nib 加载器。编辑 p3VC.m并给它一个 loadView方法:

    p3VC.m
    - (void)loadView {
    NSDictionary *externals = [NSDictionary dictionaryWithObject:[AnswerButtons answerButtons]
    forKey:@"answerButtons"];
    NSDictionary *nibOptions = [NSDictionary dictionaryWithObject:externals
    forKey:UINibExternalObjects];
    [self.nibBundle loadNibNamed:self.nibName owner:self options:nibOptions];
    [[AnswerButtons answerButtons] buttonsDidLoad];
    }

    使用这种方法,您还可以创建 IBAction AnswerButtons 中的方法类并使用 Nib 将按钮连接到这些操作。

    关于objective-c - 如何使用单例连接一组按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9146412/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com