iOS入门(二十五)基础控件

来源:互联网 发布:单片机原理及应用林立 编辑:程序博客网 时间:2024/05/22 14:22
基础控件
UILable           文本显示 

    UILabel * view1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 40, 120)];

    view1.backgroundColor = [UIColor clearColor]; //背景色

    view1.text @"i love cook";

    view1.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];

    view1.textColor = [UIColor cyanColor];

    view1.shadowColor = [UIColor brownColor];   //文本内容  、字体、颜色、阴影颜色( 属性)

    view1.shadowOffset CGSizeMake(-8, -6);   //阴影偏移量(右,下为正)

    view1.textAlignment NSTextAlignmentCenter;  //对齐方式

    view1.numberOfLines 3; //显示行数

    view1.lineBreakMode NSLineBreakByCharWrapping; //换行模式(以单词为单位换行)

    [_window addSubview:view1];

    [view1 release];



UITextField      单行输入框

    UITextField * view3= [[UITextField alloc] initWithFrame:CGRectMake(150, 50, 130, 30)];

    view3.delegate self;  //给textField 指定代理人self

    [view3 setBackgroundColor:[UIColor clearColor]];

    view3.borderStyle UITextBorderStyleRoundedRect;  //边框样式

    view3.placeholder= @"手机号/邮箱"; //预置信息

    view3.text @"手机号";     //文本信息、颜色、对齐方式、字体

    view3.textColor = [UIColor brownColor];

    view3.textAlignment NSTextAlignmentRight;

    view3.font = [UIFont fontWithName:@"Helvetica- Bold" size:20];

    view3.clearsOnBeginEditing YES;  //开始输入时是否清空内容

    view3.secureTextEntry NO;//文本输入安全

    view3.keyboardType UIKeyboardTypeDefault;    //弹出键盘类型

    view3.returnKeyType UIReturnKeyNext;  //右下角return按钮

    view3.inputView = [[UIView alloc]initWithFrame:CGRectMake(10, 300, 300, 100)];//自定义输入视图

    view3.inputAccessoryView = [[UIView alloc]initWithFrame:CGRectMake(10, 200, 300, 50)]; //输入视图上方的辅助视图,默认nil

    view3.clearButtonMode UITextFieldViewModeAlways;     //文本清除按钮

    view3.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 50, 200)];//输入框左右视图

    view3.leftViewMode UITextFieldViewModeAlways;//左右视图显示模式

    view3.tag 3;

    [_window addSubview:view3];

    [view3 release];



UIButton           按钮

    UIButton * view5 = [UIButton buttonWithType:UIButtonTypeCustom];

    view5.frame CGRectMake(20, 150, 80, 30);

    view5.backgroundColor = [UIColor clearColor];

    [view5 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateNormal];//设置指定状态下地背景图片   setImage为设置前景图片

    UIImage * normalimage = [view5 imageForState:UIControlStateNormal];//获取指定状态的前景图片

    UIImage * normalbackgroundimage = [view5 backgroundImageForState:UIControlStateNormal];//获取指定状态下的背景图片

    [view5 setTitle:@"登陆" forState:UIControlStateNormal];  //按钮改变文本

    [view5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//文本颜色

    //    [view5 setTintColor:[UIColor blackColor]];

    [view5 setShowsTouchWhenHighlighted:YES];    //闪光否

    //按钮的响应事件 对象, 运行方法,运行状态(按钮按下的时候)

    [view5 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //   [self buttonClicked:view5];

    [view5 removeTarget:self action:@selector(bundleForClass:) forControlEvents:UIControlEventTouchUpInside];//移除按钮的点击事件

    [_window addSubview:view5];



UIAlertView     提醒视图

    UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"您所拨打的号码已关机~~~" delegate:self cancelButtonTitle:@"智商问题不是你的错~" otherButtonTitles:@"我知道我不该出来~~~", @"陪我去看海吧~~~",nil];

    [alertview show];

    [alertview release];



delegate

回收键盘
(接受<<span style="color: rgb(71, 59, 255); font-family: 'Microsoft YaHei'; font-size: 13px; line-height: normal; ">UITextFieldDelegate>协议的对象在实现文件中实现)
//-(BOOL)textFieldShouldReturn:(UITextField *)textField

//{

//    //取消第一响应者

//    //作用: 回收键盘

//    [textField resignFirstResponder];

//    return YES;

//}

alertView响应点击
(接受<<span style="color: rgb(71, 59, 255); font-family: 'Microsoft YaHei'; font-size: 13px; line-height: normal; ">UIAlertViewDelegate>协议的对象在实现文件中实现)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"%d",buttonIndex);

}







确定键在右边~~~取消键在左边~~~


//登陆界面

    UIButton * view8 = [[UIButton alloc]initWithFrame:CGRectMake(00, 0, 320, 480)];

    UIImage * image3 = [UIImage imageNamed:@"20.jpg"];

    [view8 setImage:image3 forState:UIControlStateNormal];

    [_window addSubview:view8];

    UILabel * view1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 30)];

    view1.backgroundColor = [UIColor clearColor];

    //文本内容  、颜色、阴影颜色( 属性)

    view1.text @"用户名:";

    view1.textColor = [UIColor cyanColor];

    view1.shadowColor = [UIColor brownColor];

    //阴影偏移量(右,下为正)

    view1.shadowOffset CGSizeMake(-8, -6);

    //设置字体

    view1.font = [UIFont fontWithName:@"STLiti" size:25];

    //对齐方式

    view1.textAlignment NSTextAlignmentCenter;

    [_window addSubview:view1];

    [view1 release];

    UILabel * view2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 30)];

    view2.backgroundColor = [UIColor clearColor];

    view2.text @"密码:";

    view2.textColor = [UIColor cyanColor];

    view2.textAlignment NSTextAlignmentCenter;

    [_window addSubview:view2];

    [view2 release];

    UITextField * view3= [[UITextField alloc] initWithFrame:CGRectMake(150, 50, 130, 30)];

    //给textField 指定代理人self

    view3.delegate self;

    [view3 setBackgroundColor:[UIColor clearColor]];

    //边框样式

    view3.borderStyle UITextBorderStyleRoundedRect;

    //预置信息

    view3.placeholder= @"手机号/邮箱";

    //文本信息

    view3.text @"手机号";

    //开始输入时是否清空内容

    view3.clearsOnBeginEditing YES;

   //弹出键盘类型

    view3.keyboardType UIKeyboardTypeDefault;

    //文本清除按钮

    view3.clearButtonMode UITextFieldViewModeAlways;

    view3.tag 3;

    [_window addSubview:view3];

    [view3 release];

    UITextField * view4 = [[UITextField alloc]initWithFrame:CGRectMake(150, 100, 130, 30)];

    view4.backgroundColor = [UIColor clearColor];

    view4.borderStyle UITextBorderStyleLine;

    view4.placeholder @"请输入密码";

    view4.tag 4;

    //文字显示格式

     view4.secureTextEntry YES;

    [_window addSubview:view4];

    [view4 release];

    UIButton * view5 = [UIButton buttonWithType:UIButtonTypeCustom];

    view5.frame CGRectMake(20, 150, 80, 30);

    view5.backgroundColor = [UIColor clearColor];

    [view5 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateNormal];

    //按钮改变文本

    [view5 setTitle:@"登陆" forState:UIControlStateNormal];

    //文本颜色

    [view5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//    [view5 setTintColor:[UIColor blackColor]];

    //闪光否

    [view5 setShowsTouchWhenHighlighted:YES];

    //按钮的响应事件 对象, 运行方法,运行状态(按钮按下的时候)

    [view5 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

//    [self buttonClicked:view5];

    [_window addSubview:view5];

    UIButton * view6 = [[UIButton alloc]initWithFrame:CGRectMake(120, 150, 80, 30)];

    view6.backgroundColor = [UIColor blueColor];

    UIImage * image = [UIImage imageNamed:@"79.jpg"];

    [view6 setImage:image forState:UIControlStateNormal];

    [_window addSubview:view6];

    [view6 release];

    UIButton* view7 = [[UIButton alloc]initWithFrame:CGRectMake(220, 150, 80, 30)];

    view7.backgroundColor = [UIColor blueColor];

    UIImage * image2 = [UIImage imageNamed:@"27.jpg"];

    [view7 setImage:image2 forState:UIControlStateNormal];

    [_window addSubview:view7];

    [view7 release];

    

    [self.window makeKeyAndVisible];

    return YES;

}

-(void)dealloc

{

    [_window release];

    [super dealloc];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    //textfield询问代理人是否开启编辑

    return YES;

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //取消第一响应者

    //作用: 回收键盘

    [textField resignFirstResponder];

    return YES;

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"%d",buttonIndex);

}

-(void)buttonClicked:(UIButton*)button

{

    NSLog(@"登陆成功");

    button.backgroundColor = [UIColor blackColor];

    //指针强转类型之后进行操作

    NSLog(@"%@",[(UITextField *)[_window viewWithTag:4] text]);

//    NSLog(@"%@",[(UITextField *)[button . superview viewWithTag:4] text]);

    //创建ALertView

//    UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"您所拨打的号码已关机~~~" delegate:self cancelButtonTitle:@"智商问题不是你的错~" otherButtonTitles:@"我知道我不该出来~~~", @"陪我去看海吧~~~",nil];

//    [alertview show];

//    [alertview release];

    if ([[(UITextField *)[_window viewWithTag:3] text] isEqualToString:@"lanoukeji" ] && [[(UITextField *)[_window viewWithTag:4] text] isEqualToString:@"123456"]) {

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"欢迎回来~~·" message:@"你终于变聪明啦~~~" delegate:self cancelButtonTitle:@"拜拜" otherButtonTitles:@"同喜同喜",@"酱油打完了~~~",@"咱们下次一起看油菜花~", nil];

        [alert show];

        [alert release];

    }

    else{

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"危险~" message:@"用户名或者密码错误,请核对后再试~~~" delegate:self cancelButtonTitle:@"我真的智商不足~~~呜呜~" otherButtonTitles:@"姐不跟你玩了~拜!", nil];

        [alert show];

        [alert release];    }

}


0 0
原创粉丝点击