• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何通过按下 subview 中的按钮来删除以编程方式创建的 subview

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:30:03 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我目前有一个条形按钮:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle"Done" style:UIBarButtonItemStyleBordered target:self actionselector(doneDate];

它调用以下 Action :

- (IBAction)doneDateid)sender{
[self removeDateView]
}

调用如下方法:

- (void)removeDateView{

NSLog(@"subviews of view3.view: %@",self.View3.subviews);
[self.View3.subviews. makeObjectsPerformSelector: @selector(removeFromSuperview)];

}

我要删除的 subview 是

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];

目前它只是删除了该 View 中的所有内容,我似乎无法删除名为 containerView 的 View ,该 View 具有日期选择器和工具栏。



Best Answer-推荐答案


正如 erhnby 所说,您可以使用标签 - 这是一种很好的方法,但我总是尽量避免循环浏览 View 的 subview 。就我个人而言,我会制作你要删除实例变量的 View ,当你想删除它时,你可以直接在它上面调用 remove... 只是做了一个简单的例子:

.h 文件:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController {
    UIView *_containerView;
}

@end

.m 文件:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (id)init {
    self = [super init];

    // create the bar button and set it as the right bar button on the navigation bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle"Done" style:UIBarButtonItemStyleBordered target:self actionselector(removeDoneDate)];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // create the container view and add it as a subview
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)];
    _containerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_containerView];

}

- (void)removeDoneDate {
    // remove it
    [_containerView removeFromSuperview];
}

@end

结果在此开始:

enter image description here

按下按钮...

enter image description here

(抱歉,没想到白底白字这么难看)

关于ios - 如何通过按下 subview 中的按钮来删除以编程方式创建的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162201/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap