如何连接单个按钮数组?

时间:2012-02-05 02:25:11

标签: objective-c ios interface-builder

我的appDelegate,在方法didFinishLaunchingWithOptions中我加载了我的视图控制器,然后创建一个单例:

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

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

问题:如何将以这种方式创建的按钮连接到笔尖中的按钮?如果这些是简单的按钮(不是按钮数组),我会创建一个方法并使用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];
 }

3 个答案:

答案 0 :(得分:1)

由于UIButton继承自UIControl,您可以使用以下方法:

[yourButton1 addTarget:yourObject action:@selector(yourMethod) forControlEvents:UIControlEventTouchUpInside];

在创建按钮的循环中挂钩,你很高兴。如果要从界面构建器添加按钮,则需要为每个按钮挂钩IBOutlet。示例:在视图控制器init中:

 self.Ans1 = [[UIButton alloc] init];
 [Ans1 setTitle:@"" forState:UIControlStateNormal];
//set the action
    [Ans1 addTarget:self action:@selector(yourMethod) forControlEvents:UIControlEventTouchUpInside];

另一种方法是为每个按钮创建IBAction方法并将该方法挂钩到单例中的方法,在我看来更清楚。

示例:在视图控制器中:

-(IBAction) button1Press:(id)sender{

[yourSingleton button1Press];

}

答案 1 :(得分:1)

有很多方法可以做到这一点。我是通过连接笔尖中的单例连接来实现的。这是怎么回事。

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

AnswerButtons.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

请注意,我们在第一次请求单例时在类方法中创建单例。请勿在{{1​​}}中执行此操作。

接下来,让我们连接笔尖中的按钮:

  1. 打开application:didFinishLaunchingWithOptions:课程的笔尖。 (您应该考虑将此名称更改为以大写字母开头并拼出p3VCViewController。)
  2. 将“外部对象”从对象库拖到笔尖。
  3. 在Identity Inspector中,将外部对象的自定义类设置为Controller
  4. 在“属性”检查器中,将其外部对象标识符设置为AnswerButtons
  5. 按住Control键从对应按钮对象拖动到八个按钮中的每一个,将按钮连接到相应的插座。​​
  6. 最后,我们需要在加载nib时向nib加载器提供answerButtons单例。修改AnswerButtons并为其提供p3VC.m方法:

    p3VC.m

    loadView

    使用这种方法,您还可以在- (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方法,并使用nib将按钮连接到这些操作。

答案 2 :(得分:1)

要将按钮连接到界面构建器,您必须在每个变量声明中的UIButton之前放置“IBOutlet”。

IBOutlet UIButton * Ans1;

然后在界面构建器中,您可以右键单击并从视图控制器拖动到按钮上,然后选择右侧按钮。然后,如果您想让每个按钮执行一个方法,您应该在.h文件中声明一个IBAction方法:

-(IBAction)doButtonStuff:(id)sender;

然后将行为连接到界面构建器中的每个按钮,转到界面构建器并右键单击并从按钮拖动到视图控制器并选择要与其关联的方法。通常使用界面构建器既快速又简单,但如果您想在幕后做一些额外的事情,可以使用发布的代码白名单。 另外,只需注意,在设置按钮时使用较少的代码,您可以为每个循环执行一次。

for (UIButton * button in ansButs) {
    self.button = [[UIButton alloc] init];
    [button setTitle:@"" forState:UIControlStateNormal];
}

这是一个很少的代码!您必须在此代码之前声明数组并包含其中的所有按钮。

相关问题