--h文件
#import <UIKit/UIKit.h>
@interface ViewController :UIViewController
-(void) add:(UIButton*) sender;
@end
@interface myControler :ViewController
@end
--m文件
#import "ViewController.h"
@interface ViewController ()
/*只能本类实例用*/
@property (strong,nonatomic) NSMutableArray* labels;
@end
@implementation ViewController
{
/*只能本类实例用*/
int _NextY;
}
-(void) add:(UIButton *)sender
{
UILabel* lab = [[UILabelalloc] initWithFrame:CGRectMake(80,_NextY, 160,30)];
[labsetText:@"疯狂ios讲义! "];
[_labelsaddObject:lab];
[self.viewaddSubview:lab];
_NextY+=50;
}
-(void) removeLable
{
if ([_labelscount] == 0)return;
UILabel* lab =[_labelsobjectAtIndex:[_labelscount] - 1];
[lab removeFromSuperview];
[_labelsremoveObject:lab];
/*书上实现
[[_labels lastObject] removeFromSuperview];
[_labels removeLastObject];*/
_NextY-=50;
}
- (void)viewDidLoad {
[superviewDidLoad];
_NextY = 80;
/*不能用_view替换self.view,不清楚原因?*/
self.view.backgroundColor=[UIColorgrayColor];
_labels =[NSMutableArrayarray];/*这里创建的对象自动释放*/
UIButton* btnAdd = [UIButtonbuttonWithType:UIButtonTypeSystem];/*这里创建的对象自动释放*/
btnAdd.frame =CGRectMake(30,30, 60, 40);
[btnAdd setTitle:@"添加"forState:UIControlStateNormal];
[btnAdd addTarget:selfaction:@selector(add:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnAdd];
UIButton* btnRemove = [UIButtonbuttonWithType:UIButtonTypeSystem];/*这里创建的对象自动释放*/
btnRemove.frame =CGRectMake(230,30, 60, 40);
[btnRemove setTitle:@"删除"forState:UIControlStateNormal];
[btnRemove addTarget:selfaction:@selector(removeLable)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnRemove];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@implementation myControler
-(void) test
{
/*找不到父类的属性了_labels*/
/*找不到父类的成员变量了 _NextY*/
}
@end