你的分享就是我们的动力 ---﹥

学习笔记-两个UIView其间跳转、tag,UIAlertView基本使用

学习笔记--两个UIView之间跳转、tag,UIAlertView基本使用

一、两个UIView之间的跳转

//在.h文件中定义全局view_1、view_2

-(void)toView1{

if(!view_2){

[view_2 removeFromSuperview];

}


view_1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];

[self.window addSubview:view_1];


UIImageView *imageView_1 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,480)];

imageView_1.image = [UIImage imageNamed:@"pic1"];

[view_1 addSubview:imageView_1];


UIButton *button_1 = [UIButton buttonWithType:UIButtonTypeSystem];

button_1.frame = CGRectMake(0,0,320,480);

[button_1 addTarget:self action:@selector(toView2) forControlEvents:UIControlEventTouchUpInside];

[view_1 addSubview:button_1];

}


-(void)toView2{

[view_1 removeFromSuperview];

view_2 = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];

[self.window addSubview:view_2];


UIImageView *imageView_2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,480)];

imageView_2.image = [UIImage imageNamed:@"pic2"];

[view_2 addSubview:imageView_2];


UIButton *button_2 = [UIButton buttonWithType:UIButtonTypeSystem];

button_2.frame = CGRectMake(0,0,320,480);

[button_2 addTarget:self action:@selector(toView1) forControlEvents:UIControlEventTouchUpInside];

[view_2 addSubview:button_2];

}


二、tag作用:可以为控件定义一个值,来区分控件。

通过改变背景颜色测试tag的调用

-(void)clickButton{

UIButton *button_1 = [UIButton buttonWithType:UIButtonTypeSystem];

button_1.frame = CGRectMake(100,100,120,40);

[button_1 addTarget:self action:@selector(testTag:) forControlEvents:UIControlEventTouchUpInside];

button_1.tag = 10000;

[self.window addSubview:button_1];


UIButton *button_2 = [UIButton buttonWithType:UIButtonTypeSystem];

button_2.frame = CGRectMake(100,160,100,40);

[button_2 addTarget:self action:@selector(testTag:) forControlEvents:UIControlEventTouchUpInside];

button_2.tag = 20000;

[self.window addSubview:button_2];

}

-(void)testTag:(id)sender{

switch([sender tag]){

case 10000:

[self.window.backgroundColor [UIColor purpleColor]];

break;

case 20000:

[self.window.backgroundColor [UIColor grayColor]];

break;

default:

break;

}

}

三、UIAlertView提示框(仅显示,功能未实现)

定义提示框:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"有提示:" message:@"输入错误,请重试" delegate:self cancelButton:@"取消" otherButton:@"第一button",@"第二button",nil];//可定义多个

[alert show];