添加到视图的子视图的 UIButton 不会响应

Posted

技术标签:

【中文标题】添加到视图的子视图的 UIButton 不会响应【英文标题】:UIButton Added to a View's Subview Won't Respond 【发布时间】:2012-07-26 04:32:55 【问题描述】:

一直在寻找这个答案,但没有运气。从谷歌到 ***。到目前为止,我发现的唯一答案告诉人们在视图中调用 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 应该有另一个包含所有 UIButtons 的子视图 (keypadView)。所以总而言之keyboardView是UnlockKeyboards的一个子视图,所有这些按钮都应该在keyboardView里面。我可以很好地添加它们,但是当我尝试点击它们时,它们的控制状态不会改变。是的,正如你所见,我在创建 UIView 时将所有对 init 的调用都替换为 initWithFrame。

对此的任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

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

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

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

希望这对你有用

干杯,

~约翰

【讨论】:

抱歉,我忘了补充一点,当我点击它们时,按钮的控制状态不会改变,所以我假设它们没有收到任何触摸(否则我的按钮会改变它们的图片,但他们没有)。【参考方案2】:

您需要设置控件状态的图像,而不是控件状态的背景图像:

[btn setImage: [UIImage imageNamed: @"image.png"] forState: UIControlStateNormal];

所以它看起来像:

[theButton setImage:middleButtonBackgroundImage forState:UIControlStateNormal];
            [theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];

【讨论】:

我不喜欢的是它隐藏了按钮标签。 setBackgroundImage 有效,当我不将其添加到子视图时,按钮状态会发生变化。【参考方案3】:

由于没有为按钮定义目标或操作,因此按钮不知道您想要做什么。如果你确实有一个动作,你可以使用 action 方法来改变发送者的状态(发送者是按钮)。

【讨论】:

抱歉,我忘了补充一点,当我点击它们时,按钮的控制状态不会改变,所以我假设它们没有收到任何触摸(否则我的按钮会改变它们的图片,但他们没有)。 @DavidvanDugteren 的建议也很好(所以对他 +1!),但您确实需要为您的按钮设置某种目标和操作。在该操作方法中设置断点,然后您应该会看到调用该操作方法的按钮。 是的,我最终会写,但如果我的按钮状态没有改变,它可能只是意味着它没有收到任何触摸,对吧?我刚刚从子视图中删除了按钮并将它们添加到主视图中,它们改变状态就好了。 ohhhh,也许您为包含您的按钮的子视图设置了“userInteractionEnabled = NO”?明确打开它,看看会发生什么。 如果您只是将“NSLog”放入方法中并将按钮设置为调用“target = self;”并将方法作为操作,NSLogs 会被调用吗?【参考方案4】:

想通了。 我设置关于 keypadView 的按钮框架的方式完全搞砸了。当我决定将按钮添加到超级视图时,我忘记对按钮进行一些框架更改,因此按钮显示正确,“但它们的坐标不正确”就其触摸事件而言。所以实际的按钮例如在 (3,4) 中,但它的接触点在 (10,20) 中。

修复框架以更好地使用 superview 解决了我的问题

【讨论】:

以上是关于添加到视图的子视图的 UIButton 不会响应的主要内容,如果未能解决你的问题,请参考以下文章

我的子视图中的 UIButton 不起作用

如何识别 UIAutomation 中添加到 UIButton 的子视图(imageviews)?

作为 UIWindow 子视图添加的 UIView 不响应点击

以编程方式将 UIButton 添加到 UINavigationBar 的子视图,并且触摸事件未注册

自定义 UIButton + 子视图 = 无事件

我的 UIView 有一个 UIButton 作为它的子视图之一,但不响应 Objective-c 中的事件