第一个ios小程序以及实现私有成员和私有属性

--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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值