Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 随机删除不工作的自定义UIView swift 3_Ios_Swift_Uiview - Fatal编程技术网

Ios 随机删除不工作的自定义UIView swift 3

Ios 随机删除不工作的自定义UIView swift 3,ios,swift,uiview,Ios,Swift,Uiview,我正在swift 3中构建一个iOS应用程序,在这里我正在创建动态UIView。我需要随机删除自定义视图 class ViewController: UIViewController { var myView: subView! var y : CGFloat! @IBOutlet weak var addButton: UIButton! override func viewDidLoad() { y = 1 super.viewDidLoad() // Do any

我正在swift 3中构建一个iOS应用程序,在这里我正在创建动态UIView。我需要随机删除自定义视图

class ViewController: UIViewController {
var myView: subView!
var y : CGFloat!
@IBOutlet weak var addButton: UIButton!

override func viewDidLoad() {
    y = 1
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
func cancelbutton(_ sender: UIButton)
{
    myView.removeFromSuperview()
}
@IBAction func buttonAction(_ sender: Any) {
    y = y + 110
    myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))

    myView.backgroundColor = UIColor.green
    myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
    self.view.addSubview(myView)

}
上图是我的自定义视图

上面的图片是我的示例输出

当我单击“关闭”按钮时,只有一个子视图需要关闭,即选择了哪个子视图。
“提前感谢”

有一些方法可以做到这一点

我建议这样做

//一,。 创建从UIView-->继承的新类,例如“CustomView”

//二,。 在“自定义视图”标题中创建“协议”。->说“CustomViewDelegate”

@protocol CustomViewDelegate
@optional
- (void)didCloseButtonClickedWithView:(CustomView *)view;
@end
并在标题中正确委派

@property (nonatomic) id <CustomViewDelegate> delegate;
并使用“协议”方法调用委托

- (void)closeClicked:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@(didCloseButtonClickedWithView:)])
    {
        [self.delegate didCloseButtonClickedWithView:self]; // passing self is 'CustomView'
    }
}
//四,。在ViewController中创建“CustomView”对象

CustomView *view = [[CustomView alloc] initWithFrame:...];
view.delegate = self;
[self.view addSubview:view];
- (void)didCloseButtonClickedWithView:(CustomView *)view
{
    // you will get action of close button here
    // remove your view here.
    [view removeFromSuperview];

    // Additional tips:
    // Re-arrange frames for other views.
}
//五,。 在ViewController中实现CustomViewDelegate

CustomView *view = [[CustomView alloc] initWithFrame:...];
view.delegate = self;
[self.view addSubview:view];
- (void)didCloseButtonClickedWithView:(CustomView *)view
{
    // you will get action of close button here
    // remove your view here.
    [view removeFromSuperview];

    // Additional tips:
    // Re-arrange frames for other views.
}

请您尝试更准确地解释一下您想要实现的目标以及您遇到的问题。获取按钮superview并删除该视图您是否尝试单击“x”按钮并使用该按钮关闭(删除)浅绿色容器及其所有内容?如果是这样的话,你能告诉我们当你点击“x”按钮时当前发生了什么吗。提供“预期行为”和“实际行为”总是好的,这样我们就可以帮助调试和解决问题。