ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController //设置成员变量 //成员变量和属性的区别:成员属于私有;属性属于共有; { UIView * _aboveView; UIView * _downView; } @property (nonatomic,retain)NSMutableArray * aboveArray;//保存上面视图显示的按钮的名字,存储类型为NSString @property (nonatomic,retain)NSMutableArray * downArray;//保存下面视图显示的按钮的名字,存储类型为NSString @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)dealloc { [_aboveArray release]; [_downArray release]; _aboveArray = nil; _downArray = nil; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.aboveArray = [NSMutableArray array]; self.downArray = [NSMutableArray array]; [_downArray addObject:@"啊啊"]; [_downArray addObject:@"呵呵"]; [_downArray addObject:@"哈哈"]; [_downArray addObject:@"吼吼"]; [_downArray addObject:@"嘻嘻"]; [_downArray addObject:@"嘿嘿"]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self createAboveView]; [self createAboveButton]; [self createDownView]; [self createDownButton]; } //创建上边视图,用来显示点击后的按钮的内容 - (void)createAboveView { //创建一个视图叫做aboveView _aboveView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, (self.view.frame.size.height - 20.0)/2.0)];//上边栏会遮挡状态栏,所以要下移点儿y=20; [_aboveView setBackgroundColor:[UIColor purpleColor]]; [self.view addSubview:_aboveView]; [_aboveView release]; } //创建按钮,放在上面所创建的视图aboveView上 - (void)createAboveButton { //将aboveView的所有子视图从父视图上移除掉 for (UIView * aView1 in [_aboveView subviews]) { [aView1 removeFromSuperview]; } //获得aboveArray数组内容的个数,用来创建有多少个按钮添加在aboveView上面 int count = [self.aboveArray count]; for (int i = 0; i < count ; i++) { //获得X轴的坐标,因为每行显示三个按钮,所以应该通过i%3来获得余数,得到余数再乘以每个按钮的宽度与间距10的和,最后添加10为最初的与边框的间距位置 CGFloat x = (i%3)*80 + 10; //获得Y轴的坐标,因为每行最多三个按钮; CGFloat y = (i/3)*50; UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, 70, 40)]; //i为数组里面相对应的字符串的下标,通过下标取得相对应数组里面的字符串. NSString * name = [self.aboveArray objectAtIndex:i]; [button setTitle:name forState:UIControlStateNormal]; [button addTarget:self action:@selector(aboveAction:) forControlEvents:UIControlEventTouchUpInside]; [_aboveView addSubview:button]; [button release]; } } //创建下边视图,用来显示点击后的按钮的内容 - (void)createDownView { //创建一个视图叫做downView _downView = [[UIView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height- 20)/2.0+20, 320, (self.view.frame.size.height - 20.0)/2.0)]; [_downView setBackgroundColor:[UIColor blueColor]]; [self.view addSubview:_downView]; [_downView release]; } //创建按钮,放在上面所创建的视图downView上 - (void)createDownButton { //将downView的所有子视图从父视图上移除掉 for (UIView * aView in [_downView subviews]) { [aView removeFromSuperview]; } //获得downArray数组内容的个数,用来创建有多少个按钮添加在downView上面 int count1 = [self.downArray count]; for (int i = 0; i < count1 ; i++) { //获得X轴的坐标,因为每行显示三个按钮,所以应该通过i%3来获得余数,得到余数再乘以每个按钮的宽度与间距10的和,最后添加10为最初的与边框的间距位置 CGFloat x = (i%3)*80 + 10; //获得Y轴的坐标,因为每行最多三个按钮; CGFloat y = (i/3)*50; UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, 70, 40)]; //i为数组里面相对应的字符串的下标,通过下标取得相对应数组里面的字符串. NSString * name1 = [self.downArray objectAtIndex:i]; [button1 setTitle:name1 forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(downAction:) forControlEvents:UIControlEventTouchUpInside]; [_downView addSubview:button1]; [button1 release]; } } //创建一个按钮的响应事件,让按钮上去 - (void)downAction:(id)sender { UIButton * button1 = (UIButton *)sender;//强转(UIButton *) NSString * name = button1.currentTitle; //根据名字获得其在数组中的下标 NSInteger index = [self.downArray indexOfObject:name]; //将名字从数组中删除 [self.downArray removeObjectAtIndex:index]; //将名字放到上面的aboveArray的数组中 [self.aboveArray addObject:name]; [self createDownButton]; [self createAboveButton]; } //创建一个按钮的响应事件,让按钮下去 - (void)aboveAction:(id)sender { UIButton * button1 = (UIButton *)sender;//强转(UIButton *) NSString * name = button1.currentTitle; //根据名字获得其在数组中的下标 NSInteger index = [self.aboveArray indexOfObject:name]; //将名字从数组中删除 [self.aboveArray removeObjectAtIndex:index]; //将名字放到上面的downArray的数组中 [self.downArray addObject:name]; [self createDownButton]; [self createAboveButton]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end