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)}
上图是我的自定义视图
上面的图片是我的示例输出
当我点击关闭按钮时,只有一个子视图想要关闭,我选择了哪一个.
“提前致谢”
我会建议这个.
// 1
创建一个继承自UIVIEw的新类 – >说’CustomVIEw’.
// 2
在“CustomVIEw”标题中创建“协议”. – >说’CustomVIEwDelegate’
@protocol CustomVIEwDelegate@optional- (voID)dIDClosebuttonClickeDWithVIEw:(CustomVIEw *)vIEw;@end
并在标题中正确委托.
@property (nonatomic) ID <CustomVIEwDelegate> delegate;
// 3
在CustomVIEw.m中 – >
添加“关闭”按钮的 *** 作. (通过故事板或以编程方式,无论您喜欢什么).
- (voID)closeClicked:(UIbutton *)button{}
并使用“协议”方法调用委托,
- (voID)closeClicked:(UIbutton *)button{ if (self.delegate && [self.delegate respondsToSelector:@(dIDClosebuttonClickeDWithVIEw:)]) { [self.delegate dIDClosebuttonClickeDWithVIEw:self]; // passing self is 'CustomVIEw' }}
// 4.在VIEwController中制作’CustomVIEw’对象.
CustomVIEw *vIEw = [[CustomVIEw alloc] initWithFrame:...];vIEw.delegate = self;[self.vIEw addSubvIEw:vIEw];
// 5
在VIEwController中实现CustomVIEwDelegate.
- (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.}总结
以上是内存溢出为你收集整理的ios – 随机删除自定义UIView不工作swift 3全部内容,希望文章能够帮你解决ios – 随机删除自定义UIView不工作swift 3所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)