Last active
April 8, 2016 14:03
-
-
Save nikolasburk/8f0589e1641ff8be888e75b39b6a9a45 to your computer and use it in GitHub Desktop.
`weak` vs `strong` declaration of subview property of a `UIViewController` (http://stackoverflow.com/questions/36495834/is-the-need-to-declare-a-uibutton-property-in-a-uiviewcontroller-as-strong-a-bug)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// ButtonTest | |
// | |
// Created by Nikolas Burk on 08/04/16. | |
// Copyright © 2016 Nikolas Burk. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@property (nonatomic, strong) UIButton *someButton; | |
@property (nonatomic, weak) UITextField *someTextField; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self.someButton setCenter:self.view.center]; | |
[self.view addSubview:self.someButton]; | |
[self.someTextField setCenter:CGPointMake(self.view.center.x, self.view.center.y/2.0)]; | |
[self.view addSubview:self.someTextField]; | |
NSLog(@"subviews: %@", self.view.subviews); | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
} | |
- (UIButton *)someButton | |
{ | |
if (!_someButton) { | |
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
CGRect someButtonFrame = CGRectMake(0.0, 0.0, 100.0, 44.0); | |
someButton.frame = someButtonFrame; | |
[someButton setTitle:@"Do something" forState:UIControlStateNormal]; | |
[someButton addTarget:self action:@selector(someButtonPressed) forControlEvents:UIControlEventTouchUpInside]; | |
_someButton = someButton; | |
} | |
return _someButton; | |
} | |
- (UITextField *)someTextField | |
{ | |
if (!_someTextField) { | |
CGRect someTextFieldFrame = CGRectMake(0.0, 0.0, 250.0, 44.0); | |
UITextField *someTextField = [[UITextField alloc] initWithFrame:someTextFieldFrame]; | |
someTextField.layer.borderWidth = 1.0; | |
someTextField.layer.borderColor = [UIColor blackColor].CGColor; | |
_someTextField = someTextField; | |
} | |
return _someTextField; | |
} | |
- (void)someButtonPressed | |
{ | |
NSLog(@"button pressed..."); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment