Skip to content

Instantly share code, notes, and snippets.

@aug2uag
Created May 17, 2014 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aug2uag/5378c17c8874e5dbca10 to your computer and use it in GitHub Desktop.
Save aug2uag/5378c17c8874e5dbca10 to your computer and use it in GitHub Desktop.
demonstration that removing object from superview requires re-alloc of that object
//
// ViewController.m
// remove from superview demo
//
// Created by Rex Fatahi on 5/17/14.
// Copyright (c) 2014 aug2uag. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
UIView* aView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 240, 240)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:@"do it" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(doIT:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(20, self.view.bounds.size.height - 64, 280, 44);
[self.view addSubview:aButton];
}
- (void)doIT:(UIButton *)sender
{
if (!sender.selected) {
[aView removeFromSuperview];
} else {
[self.view addSubview:aView];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment