UIButton如何从UIView中删除?

dan*_*ser 1 iphone uibutton uiview

我已经设置了一个按钮并将其添加到视图中.我想在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];
    }
Run Code Online (Sandbox Code Playgroud)

如果我有一个类型为NumbersAndPunctuation的Kayboard,我想删除按钮,一切都很好.

如果我单击我[(UIButton)*sender removeFromSuperview]; 用来防止内存泄漏的按钮.

但是如何从其他功能中删除按钮?

非常感谢!

其他一些人确实在其他地方问了这个问题,但没有得到答案.我相信你可以帮忙:)

joh*_*ope 7

// 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];
    }
}
Run Code Online (Sandbox Code Playgroud)