制作由多个视图控制器调用的自定义数字键盘类

时间:2013-12-20 10:35:44

标签: ios objective-c custom-controls

我是Objective-c和iOS编程的新手。我想使用自定义数字键盘进行游戏输入。有几种类型的游戏,(game1 game2 game3),游戏可能需要更多或更少的数字才能显示在数字键盘(控制)上。 每个游戏规则和显示属性都写在单独的视图控制器类中。我尝试将数字键盘的副本从game1剪切并粘贴到其他控制器。它会抛出一个错误(在构建时),说我正在使用的数字按钮属性的名称已被使用。

这是否需要我重命名并重新链接每个游戏的所有对象和属性。对我来说有很多维护费用。所以我一直在尝试制作一个源自NSObject的NumPad类。 这工作正常,直到我尝试从非视图控制器类创建视图/容器视图。我在game1视图控制器的viewdidload部分中实例化numpad对象。这是我到目前为止所做的。

.h文件

#import <Foundation/Foundation.h>
#import "NumberPadTestAppDelegate.h"

@interface NumPad : NSObject


-(void)numberPadSetUp: (int) numberOfButtons;

@end

.m文件

#import "NumPad.h"

@implementation NumPad

-(void)numberPadSetUp: (int) numberOfButtons
{
      // Instantiate a Container View by code??? test.
   CGRect frame = CGRectMake(14, 47, 740, 370);
   UIView *Mycontainer = [[UIView alloc] initWithFrame:frame];
   Mycontainer.backgroundColor = [UIColor blueColor];
   [Mycontainer addSubview:Mycontainer];

      // This was here for testing before adding the above four lines.
   for (int a = 1; a < numberOfButtons +1; a++)
        {
        NSLog(@"Button %i has been added.",a);
        }
   NSLog(@"  ");
   NSLog(@"Numberpad setup is complete.");
} // End of "numberPadSetUp" routine.

@end
来自game1测试控制器的

.m文件

#import "NumberPadTestViewController.h"

@interface NumberPadTestViewController ()

@end

@implementation NumberPadTestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

   NumPad *MyNumberPad =[[NumPad alloc]init];

   [MyNumberPad numberPadSetUp:9];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

此代码提供了写入的运行时错误。

int main(int argc, char * argv[])
{
   @autoreleasepool {
       return UIApplicationMain(argc, argv, nil, NSStringFromClass([NumberPadTestAppDelegate class]));
   }
}

我还想知道在IB中使用容器视图控件是否可以完成任务,或者我是否可以获得与以前相同的命名错误? 我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

问题出在这一行

[Mycontainer addSubview:Mycontainer];

您宣布Mycontainer并将其添加到自身。你应该写下像

这样的课程
#import "NumPad.h"

@implementation NumPad

#define kButtonWidth 50
#define kButtonHeight 15
#define kPadding 20

-(void)numberPadSetUp: (int) numberOfButtons
{
    // Instantiate a Container View by code??? test.
    // This was here for testing before adding the above four lines.
    int xPos = 0;
    int yPos = 0;
    for (int a = 1; a < numberOfButtons +1; a++)
    {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
        CGRect aButtonFrame = CGRectMake(xPos, yPos, kButtonWidth, kButtonHeight);
        aButton.frame = aButtonFrame;
        [aButton setTitle:[NSString stringWithFormat:@"%d",a] forState:UIControlStateNormal];
        [self addSubview:aButton];

        xPos+=kButtonWidth+kPadding;

        if(fmod(a, 3)==0){
            yPos+=kButtonHeight+kPadding;
            xPos = 0;
        }


        NSLog(@"Button %i has been added.",a);
    }
    NSLog(@"  ");
    NSLog(@"Numberpad setup is complete.");
} // End of "numberPadSetUp" routine.


@end

在你的主要班级

NumPad *MyNumberPad =[[NumPad alloc]initWithFrame:CGRectMake(14, 47, 740, 370)];
[MyNumberPad numberPadSetUp:9];
[self.view addSubview:MyNumberPad];
[self.view bringSubviewToFront:MyNumberPad];

这将为您提供iOS6 / iOS7中的以下图像

的结果

iOS6 iOS7