objective-c - 添加到 View 的 subview 的 UIButton 不会响应

标签 objective-c ios uiview uikit addsubview

一直在寻找这个答案,但没有成功。从谷歌到 StackOverflow。到目前为止,我找到的唯一答案是告诉人们在 View 中调用 initWithFrame 而不仅仅是 init。我用 initWithFrame 替换了 UIViews 中对 init 的所有调用,但我没有任何运气。

我没有使用 IB。

这是我的全部代码:

@interface UnlockKeyboard : UIView
{
    NSArray *buttons;
    UITextField *passcodeFields;

    UIImage *buttonBackgroundImage;
    UIImage *buttonBackgroundHighlightedImage;
    UIImage *middleButtonBackgroundImage;
    UIImage *middleButtonBackgroundImageHighlighted;
    UIImage *screenBackgroundImage;
    UIImage *keypadViewBackgroundImage;

    UIView *infoView;
    UIView *keypadView;
}
@property(nonatomic, retain)UIImage *buttonBackgroundImage;
@property(nonatomic, retain)UIImage *buttonBackgroundHighlightedImage;
@property(nonatomic, retain)UIImage *screenBackgroundImage;
@property(nonatomic, retain)UIImage* keypadViewBackgroundImage;

@end

@implementation UnlockKeyboard
@synthesize buttonBackgroundImage = _buttonBackgroundImage;
@synthesize buttonBackgroundHighlightedImage = _buttonBackgroundHighlightedImage;
@synthesize screenBackgroundImage = _screenBackgroundImage;
@synthesize keypadViewBackgroundImage = _keypadViewBackgroundImage;
-(id)init
{
    if((self = [super initWithFrame:CGRectMake(0, 0, 320, 480)]))
    {
        _unlockKeyboardBundle = [NSBundle bundleForClass:[self class]];
        buttonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"button" ofType:@"png"]];
        middleButtonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"middleButton" ofType:@"png"]];
        buttonBackgroundHighlightedImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"pushedButton" ofType:@"png"]];
        middleButtonBackgroundImageHighlighted = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"pushedButtonMiddle" ofType:@"png"]];
        keypadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        NSMutableArray *tempButtons = [NSMutableArray array];
        //self.frame = CGRectMake(320, 480, 0, 0);
        self.backgroundColor = [UIColor lightGrayColor];
        self.opaque = YES;

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setTitle:@"1" forState:UIControlStateNormal];
        button1.frame = CGRectMake(0, 261, 106, 50);

        UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button4 setTitle:@"4" forState:UIControlStateNormal];
        button4.frame = CGRectMake(0, 311, 106, 50);

        UIButton *button7 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button7 setTitle:@"7" forState:UIControlStateNormal];
        button7.frame = CGRectMake(0, 361, 106, 50);

        UIButton *hint = [UIButton buttonWithType:UIButtonTypeCustom];
        [hint setTitle:NSLocalizedString(@"Hint", @"Hint string") forState:UIControlStateNormal];
        hint.frame = CGRectMake(0, 411, 106, 50);

        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button2 setTitle:@"2" forState:UIControlStateNormal];
        button2.frame = CGRectMake(106, 261, 108, 50);

        UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button5 setTitle:@"5" forState:UIControlStateNormal];
        button5.frame = CGRectMake(106, 311, 108, 50);

        UIButton *button8 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button8 setTitle:@"8" forState:UIControlStateNormal];
        button8.frame = CGRectMake(106, 361, 108, 50);

        UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button0 setTitle:@"0" forState:UIControlStateNormal];
        button0.frame = CGRectMake(106, 411, 108, 50);

        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button3 setTitle:@"3" forState:UIControlStateNormal];
        button3.frame = CGRectMake(214, 261, 106, 50);

        UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button6 setTitle:@"6" forState:UIControlStateNormal];
        button6.frame = CGRectMake(214, 311, 106, 50);

        UIButton *button9 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button9 setTitle:@"9" forState:UIControlStateNormal];
        button9.frame = CGRectMake(214, 361, 106, 50);

        UIButton *cancelOrDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelOrDelete setTitle:@"<-" forState:UIControlStateNormal];
        cancelOrDelete.frame = CGRectMake(214, 411, 108, 50);

        [tempButtons addObject:button1];
        [tempButtons addObject:button2];
        [tempButtons addObject:button3];
        [tempButtons addObject:button4];
        [tempButtons addObject:button5];
        [tempButtons addObject:button6];
        [tempButtons addObject:button7];
        [tempButtons addObject:button8];
        [tempButtons addObject:button9];
        [tempButtons addObject:button0];
        [tempButtons addObject:hint];
        [tempButtons addObject:cancelOrDelete];

        for(UIButton *theButton in tempButtons)
        {
            if([theButton.currentTitle isEqualToString:@"2"] || [theButton.currentTitle isEqualToString:@"5"] || [theButton.currentTitle isEqualToString:@"8"] || [theButton.currentTitle isEqualToString:@"0"])
            {
                [theButton setBackgroundImage:middleButtonBackgroundImage forState:UIControlStateNormal];
                [theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
            }else
            {
                [theButton setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
                [theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
            }
            [keypadView addSubview:theButton];
        }
        [self addSubview:keypadView];

        [pool drain];
    }
    return self;
}
-(void)dealloc
{
    [buttons release];
    [passcodeFields release];
    [buttonBackgroundImage release];
    [buttonBackgroundHighlightedImage release];
    [screenBackgroundImage release];
    [infoView release];
    [keypadView release];
    [super dealloc];
}

如您所见,我将 UIView 子类化为 UnlockKeyboard。 UnlockKeyboard 应该有另一个包含所有 UIButton 的 subview (keypadView)。所以总而言之,keyboardView 是 UnlockKeyboards 的 subview ,所有这些按钮都应该在 keyboardView 中。我可以很好地添加它们,但是当我尝试点击它们时,它们的控制状态不会改变。是的,正如您所见,我在创建 UIView 时用 initWithFrame 替换了所有对 init 的调用。

我们将不胜感激。

最佳答案

您需要为按钮添加目标,以便它们知道在您按下它们时调用什么方法。它看起来像这样

[button addTarget:self action:@selector(method) forControlEvents:UIControlEventTouchUpInside];

其中 self 是包含方法的实例,方法是单击按钮时运行的方法。

希望这对你有用

干杯,

~约翰

关于objective-c - 添加到 View 的 subview 的 UIButton 不会响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11662377/

相关文章:

ios - "NSString stringWithFormat:"和 "%zd"在 32 位设备中的奇怪行为

PHP Apple Enhanced Push Notification读取错误响应

iphone - 如何在改变方向时完全隐藏 UIView?

ios - 为什么尽管使用了 `insertSubview:belowSubview:`,我的 View 仍然出现在另一个 View 之上?

objective-c - UIView动画VS核心动画

objective-c - 如果我们使用系统字体,如何在 iOS 7 中重新排列字体大小?

ios - 如何在 iOS 中终止应用程序后继续在后台下载

objective-c - 什么是 int foo[] = {1200,100} 以及如何在 Swift 中重新创建它?

objective-c - 从我自己的包中加载 Nib ?

iOS 11 密码自动填充 - 如何查找用户在其钥匙串(keychain)中选择了哪个凭据提供程序?