iOS 修飾符strong和weak

舉個栗子:

@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都不會出現在界面了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容