我已经设置了一个Button并将其添加到视图中。我想在UIKeyboardTypeNumberPad中添加一个“完成”按钮。这是我的代码。
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}例如,如果我有一个NumbersAndPunctuation类型的Kayboard,在我想移除按钮之前,一切都很好。
如果我单击该按钮,我将使用[(UIButton)*sender removeFromSuperview];来防止内存泄漏。
但是如何从其他函数中删除按钮呢?
非常感谢!
其他一些人确实在其他地方问了这个问题,但没有得到答案。我相信你能帮上忙:)
发布于 2010-01-13 22:00:16
您应该存储对按钮的引用,而不是使用局部变量。例如:
头文件:
@interface myObject : NSObject {
UIButton *doneButton;
...实现文件:
doneButton = [UIButton buttonWithType: UIButtonTypeCustom
...要删除它,请执行以下操作(假设您在同一对象中:
[doneButton removeFromSuperview];然而,苹果可能不会接受你在他们的键盘上添加按钮。
发布于 2011-04-11 14:20:42
// Where self is a UIView subclass
NSLog(@"subviews: %@",self.subviews);
for(id view in self.subviews ){
if ([view isKindOfClass:[UIButton class]]) {
NSLog(@"Removing a button!");
[view removeFromSuperview];
}
}发布于 2010-01-13 22:02:45
您可以在.h文件中声明按钮,这样就可以从所有类方法中获得访问权限
https://stackoverflow.com/questions/2057062
复制相似问题