父视图上按钮的添加移除

//

//  ViewController.m

//  UIScrollerview2

//

//  Created by 红珊瑚 on 15/5/10.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()

{

    

    UIScrollView *oo;

    

    UIButton *But2;

    UIButton *But3;

    int j;

    

}

@end


@implementation ViewController


- (void)viewDidLoad

   {

       [super viewDidLoad];

       

       oo=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 17, 320, 480)];

       oo.backgroundColor=[UIColor yellowColor];

       [self.view addSubview:oo];

       

       for(int i=0; i<6; i++)

       {

          UIButton *But1=[[UIButton alloc]initWithFrame:CGRectMake(0, i/1*80, 320, 78)];

           But1.backgroundColor=[UIColor blueColor];

           [oo addSubview:But1];

           

           [But1 setTitle:[NSString stringWithFormat:@"%d",i+1]

                forState:UIControlStateNormal];//添加数字到按钮

           But1.tag = i+1;

           [But1 addTarget:self action:@selector(selet:) forControlEvents:UIControlEventTouchUpInside];

       }

       oo.contentSize=CGSizeMake(320, 50*80);

      

       

       But2=[[UIButton alloc]initWithFrame:CGRectMake(6, 509, 150, 50)];

       But2.backgroundColor=[UIColor orangeColor];

       [self.view addSubview:But2];

       But3=[[UIButton alloc]initWithFrame:CGRectMake(163, 509, 150, 50)];

       But3.backgroundColor=[UIColor magentaColor];

       [self.view addSubview:But3];

       

       [But2 addTarget:self action:@selector(selet1) forControlEvents:UIControlEventTouchUpInside];//给按扭添加功能

       [But3 addTarget:self action:@selector(selet2) forControlEvents:UIControlEventTouchUpInside];//给按扭添加功能


       

       

       j=6;

       

   }

-(void)selet:sender

{

    UIButton  *btn= sender;

    [btn removeFromSuperview];

   // [[oo viewWithTag:btn.tag] removeFromSuperview];

    

}



-(void)selet1

{

    //

        UIButton *But1=[[UIButton alloc]initWithFrame:CGRectMake(0, j*80, 320, 78)];

        But1.backgroundColor=[UIColor blueColor];

        [oo addSubview:But1];

        

        [But1 setTitle:[NSString stringWithFormat:@"%d",j+1]

              forState:UIControlStateNormal];//添加数字到按钮

        But1.tag = j+1;

    

 j++;

}

/*{

    

    for(int i=0; i<j; i++)

    {

        UIButton *But=(UIButton *)[oo viewWithTag:i+1];

        [But removeFromSuperview];

        But=nil;

        

    }

    

j++;

    

    for(int i=0; i<j; i++)

    {

        UIButton *But1=[[UIButton alloc]initWithFrame:CGRectMake(0, i/1*80, 320, 78)];

        But1.backgroundColor=[UIColor blueColor];

        [oo addSubview:But1];

        

        [But1 setTitle:[NSString stringWithFormat:@"%d",i+1]

              forState:UIControlStateNormal];//添加数字到按钮

        But1.tag = i+1;

    }

    

}*/



-(void)selet2

{

    j--;

    

//    for(int i=0; i<j; i++)

//    {

       UIButton *But=(UIButton *)[oo viewWithTag:j+1];//?

       [But removeFromSuperview];

        But=nil;

//        

//    }

    


    

//            UIButton *But1=[[UIButton alloc]initWithFrame:CGRectMake(0, j/1*80, 320, 78)];

//        But1.backgroundColor=[UIColor blueColor];

//        [oo addSubview:But1];

//        

//        [But1 setTitle:[NSString stringWithFormat:@"%d",j+1]

//              forState:UIControlStateNormal];//添加数字到按钮

//        But1.tag = j+1;

    

   


}

//-(void)select:sender

/*{

    UIButton *But1=sender;

    

    if ([But1.backgroundColor isEqual:[UIColor blueColor]])

    {

        But1.backgroundColor=[UIColor blackColor];

    }

    else

        if ([But1.backgroundColor isEqual:[UIColor blackColor]])

        {

            But1.backgroundColor=[UIColor redColor];

        }

   else

       if ([But1.backgroundColor isEqual:[UIColor redColor]]) {

           But1.backgroundColor=[UIColor blueColor];

       }

}*/







@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值