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