举个栗子:
@interface TestViewController ()
@property (nonatomic,strong) UIButton *strongButton;
@property (nonatomic,weak) UIButton *weakButton;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = NO;
self.strongButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.strongButton.backgroundColor = [UIColor blackColor];
[self.strongButton setTitle:@"强按钮" forState:UIControlStateNormal];
[self.strongButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.strongButton addTarget:self action:@selector(strongButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.strongButton];
[self.strongButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(@100);
make.size.mas_equalTo(CGSizeMake(80, 30));
}];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"弱按钮" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(weakButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.weakButton = button;
[self.view addSubview:self.weakButton];
[self.weakButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(@150);
make.size.mas_equalTo(CGSizeMake(80, 30));
}];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"测试按钮1" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(button1Click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(@200);
make.size.mas_equalTo(CGSizeMake(80, 30));
}];
UIButton *btnn = [UIButton buttonWithType:UIButtonTypeCustom];
btnn.backgroundColor = [UIColor blackColor];
[btnn setTitle:@"测试按钮2" forState:UIControlStateNormal];
[btnn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnn addTarget:self action:@selector(button2Click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnn];
[btnn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(@250);
make.size.mas_equalTo(CGSizeMake(80, 30));
}];
}
#pragma mark - action
- (void)strongButtonClick:(UIButton *)sender{
[self.strongButton removeFromSuperview];
}
- (void)weakButtonClick:(UIButton *)sender{
[self.weakButton removeFromSuperview];
}
- (void)button1Click:(UIButton *)sender{
[self.view addSubview:self.strongButton];
[self.view addSubview:self.weakButton];
}
- (void)button2Click:(UIButton *)sender{
[self.strongButton removeFromSuperview];
self.strongButton = nil;
}
运行:
点击 强按钮(- (void)strongButtonClick:(UIButton *)sender 有断点),看到两个按钮都在
跑完该方法后看界面:
继续点击 弱按钮
我们再点击测试按钮1(- (void)button1Click:(UIButton *)sender 有断点)
可以看到_strongButton还是存在的,而_weakButton为nil。我们po一下
方法走完,发现界面
强按钮的frame和我们po出来的不一样。但是_strongButton出现在界面上了,而_weakButton = nil 不会出现在界面上
我们再点击 测试按钮2 ,跑完self.strongButton = nil;后
再点击 测试按钮1,_strongButton和_weakButton都不会出现在界面了。