【问题标题】:Problem adding subviews (UIButtons) to subclass of UIImageView将子视图(UIButtons)添加到 UIImageView 的子类时出现问题
【发布时间】:2023-03-23 22:18:01
【问题描述】:

我正在向我的应用程序添加一个自定义按钮/键盘。到目前为止,我有一个 UIImageView 子类,其中包含一个动画,它从屏幕底部滑动它,然后在用户不再需要它时返回。但是,我无法将 UIButtons 添加到此 UIImageView。

由于这是 UIView 的子类,我试图通过 initWithFrame: 方法将按钮添加到我的视图中。 (slideDown方法是添加的动画)

在我的 UIImageView 子类中,我添加了一个 UIButton ivar 对象:

-(id)initWithFrame:(CGRect)frame {

  if (self = [super initWithFrame: frame]) {
  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  button.frame = CGRectMake(16.0, 20.0, 50.0, 50.0);
  [button setTitle: @"Go" forState: UIControlStateNormal];
  [button addTarget: self action: @selector(slideDown) forControlEvents: UIControlEventTouchUpInside];
  self.button1 = button;
   [self addSubview: button1];
   NSLog(@"Button added");

}
return self;
}

在我的视图控制器中,我在 -(void)viewDidLoad: 方法中实例化我的 UIIMageView 子类,如下所示:

-(void)viewDidLoad {
//other objects init'ed

ButtonPad *customPad = [[ButtonPad alloc] initWithImage: [UIImage imageNamed: @"ButtonPad.png"]];
customPad.frame = CGRectMake(0.0, 480.0, 320.0, 300.0);
self.buttonPad = customPad;

[self.view addSubview: buttonPad];
[customPad release];  

[super viewDidLoad];
}

我当前的应用程序允许视图在屏幕上上下滑动,没有任何问题。但是,该按钮永远不会出现。我还尝试通过实例化并将其作为子视图添加到我的视图控制器文件中的 buttonPad 来将按钮添加到我的 buttonPad 对象。这有效......但它不允许按钮起作用。

我想知道: A.) 将按钮或任何子视图添加到 UIView initWithFrame: 方法是否合适,或者我应该将这些子视图作为子视图添加到视图控制器文件中的 buttonPad 中? B.) 由于我正在创建自定义按钮/键盘,我是通过使用普通的 UIViewController 来遵循有效的方法,还是应该使用模态视图控制器之类的东西? (我对这些知之甚少。)

【问题讨论】:

  • 我认为你不应该释放按钮。 +buttonWithType: 应该给你一个自动释放的对象。
  • 好收获。我改变了,但仍然看不到按钮。我已经尝试了 UIButtonTypeRoundedRect 和 UIButtonTypeCustom 都无济于事。

标签: iphone cocoa-touch uiview uiimageview uibutton


【解决方案1】:

我看到的一个小错误:

UIbutton *button = [[UIButton alloc] initWithFrame:CGRectMake(16.0, 20.0, 50.0, 50.0);
button.buttonType = UIButtonTypeRoundedRect;

将其添加到说的地方:

 UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

这个错误导致按钮甚至永远不会被分配,如果它不起作用,您可能需要将按钮放在UIView 内。 希望这会有所帮助。

【讨论】:

  • 没有使用:button.buttonType = UIButtonTypeRoundedRect;只读属性?这就是文档所说的。
  • 再看一遍后,我看到你说要把按钮放在 UIView 中。我对你所说的感到困惑,因为这就是我想要做的;也就是在 UIView 中添加一个 UIButton。您的示例代码是否高于修复它的建议或引用我的错误?感谢您提供的任何澄清。
  • 我编辑了我的答案,应该对你有所帮助。我只是想让你知道。另外,我对 UIView 的看法是,您需要将其添加到 UIView 而不是 UIImageView 因为 UIImageView 是图像,当您将按钮放入图像时,它只是按钮的图像而不是可用性的一个按钮。添加一个 UIView 并使用 UIImageView 使其具有动画效果,并将 UIButton 添加到 UIView 中,以便您仍然拥有该按钮并且它是可用的。
  • 感谢您的澄清。解决方案是在 initWithFrame: 方法中将我的 UIImageView 添加到我的 UIView 中,然后在其上添加所有按钮。
【解决方案2】:

您是从 nib 文件加载您的 UIImageView 子类吗?如果是,则不会调用-initWithFrame:,而是调用-initWithCoder:

我通常会这样做:

- (void)didInit {
    // add your buttons here
}
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self didInit];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self didInit];
    }
    return self;
}

【讨论】:

  • 我没有使用 nib 文件.. 所以这不是问题。
  • 只要确保 -initWithFrame 确实被调用。
【解决方案3】:

突破:我注意到的一个问题是,在我的视图控制器中,我使用 initWithImage 来实例化我的 UIImageView 而不是使用 initWithFrame: 导致图像被加载,而不是按钮。现在按钮出现了,但不起作用。

视图控制器文件现在看起来像这样:

-(void)viewDidLoad {
//other objects init'ed here.

ButtonPad *customPad = [[ButtonPad alloc] initWithFrame: CGRectMake(0.0, 480.0, 320.0, 300.0)];
customPad.image = [UIImage imageNamed: @"ButtonPad.png"];
self.buttonPad = customPad;

[self.view addSubview: buttonPad];
[customPad release];

[super viewDidLoad];  
}

这将分配和初始化框架,该框架使用来自我的 UIIMageView 子类的重写方法。任何有关使按钮工作的帮助将不胜感激。

【讨论】:

    【解决方案4】:

    我认为您的问题是 UIImageView 默认禁用其 userInteractionEnabled 属性。您可以尝试添加行
    customPad.userInteractionEnabled = true;
    到初始化和设置。

    【讨论】:

    • 是的 - 这就是诀窍:为了使用 UIImageView 中的 UIButton,您需要允许 UIImageView 上的用户交互。
    • 我建议删除图像,然后检查按钮是否按预期反应,以确保是图像而不是按钮导致问题。然后,您需要检查您的图像和按钮边界/框架,以确保它们可以正确选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多