【发布时间】: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