21.View,Label,Button,TextField

本文深入探讨了iOS开发中的Swift编程语言,包括基础语法、常用框架、性能优化及实战案例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.UIView

//1.创建一个UIView的对象
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//2.view1设置背景颜色
view1.backgroundColor = [UIColor cyanColor];
//3.把试图贴到窗口上
[self.window addSubview:view1];
//4.释放
[view1 release];
//试图添加到父视图的数组之后,数组会增加视图的引用计数,相应的也就可在添加之后对视图进行释放
//视图的坐标起始位置在父视图的左上角,一个视图可以有多个子视图,但是一个视图只能有一个父视图
//先创建的先添加到superview的视图会在层级关系的最下面,通过父视图来管理它身上所有的子视图的层级关系
[self.window bringSubviewToFront:view2];
//父视图把指定的视图放在最上面/下面
[self.window sendSubviewToBack:view2];
//透明度
view2.alpha = 0.5;
//tag不能是0,不能重复
view3.tag = 100;
//通过tag找到对应的view
UIView *tempView = [self.window viewWithTag:100];

2.UILabel

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 150, 200)];
label.backgroundColor = [UIColor orangeColor];
[self.window addSubview:label];
//设置文本内容
label.text = @"fafdfasdfdasfdsa]";
//设置行数,默认是一行,设置为0是行数的最大值
label.numberOfLines = 3;
//让文本自己去适应label的尺寸,显示全部内容
[label sizeToFit];//--->**
//文本的对齐方式(默认左对齐)
label.textAlignment = NSTextAlignmentCenter;
//断行模式
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
//文本内容的颜色
label.textColor = [UIColor redColor];
//阴影颜色
label.shadowColor = [UIColor redColor];
//阴影大小
label.shadowOffset = CGSizeMake(1, 2);
//设置边框
label.layer.borderWidth = 1;//--->**
//设置圆角
label.layer.cornerRadius = 5;//--->**
//隐藏多余的部分
label.layer.masksToBounds = YES;//--->**
//字体大小
label.font = [UIFont systemFontOfSize:20];
//center可以改变视图的位置
label.center = CGPointMake(150, 150);
[label release];

3.UIButton

//用button自己的便利构造器的方式来创建对象
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//指定button的位置和大小
button.frame = CGRectMake(30, 50, 100, 30);
//设置背景颜色
button.backgroundColor = [UIColor redColor];
//给button设置标题
[button setTitle:@"点我啊" forState:UIControlStateNormal];
[button setTitle:@"hehe" forState:UIControlStateHighlighted];
//设置字体大小
button.titleLabel.font = [UIFont systemFontOfSize:18];//--->**
//设置一下边框和圆角,隐藏多余的部分
button.layer.borderWidth = 0.1;//--->**
button.layer.cornerRadius = 10;//--->**
button.layer.masksToBounds = YES;//--->**
[self.window addSubview:button];
//button不用release//--->**//--->**//--->**

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(40, 40, 30, 30);
//给button设置背景图片,图片大小由frame决定,填满整个frame
[button1 setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
[self.window addSubview:button1];
[button1 addTarget:self action:@selector(chageImage:) forControlEvents:UIControlEventTouchUpInside];
self.isClick = NO;//isClick为BOOL类型的属性


UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(120, 40, 51, 24);
//如果想使用setImage设置图片的话,button的类型要调整成custom,setImage方法不会把图片放大成按钮大小,但是如果按钮的frame比图片小,则会缩小成按钮大小
[button2 setImage:[UIImage imageNamed:@"BtnOn"] forState:UIControlStateNormal];
button2.tag = 100;
[self.window addSubview:button2];
self.isSelected = YES;//isSelected为BOOL类型的属性
//点击方法 button最重要的方法
[button2 addTarget:self action:@selector(chgngePic:) forControlEvents:UIControlEventTouchUpInside];

- (void)chageImage:(UIButton *)button
{
    if (self.isClick) {
        [button setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
    }
    else{
        [button setBackgroundImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    }
    self.isClick = !self.isClick;
}

- (void)chgngePic:(UIButton *)button//button为传送过来的一个参数
{
    if (self.isSelected) {
        [button setImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateNormal];
    }
    else{
        [button setImage:[UIImage imageNamed:@"BtnOn"] forState:UIControlStateNormal];
    }
    self.isSelected = !self.isSelected;
}

4.UITextField

//输入框UITextFiled
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 60, 200, 40)];
textField.backgroundColor = [UIColor redColor];
//加上圆角和边框,隐藏多余的部分
textField.layer.borderWidth = 1;
textField.layer.cornerRadius = 5;
label.layer.masksToBounds = YES;
//UITextField系统提供的属性
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = @"zhangsan";
textField.textColor = [UIColor greenColor];
textField.textAlignment = NSTextAlignmentCenter;
textField.font = [UIFont systemFontOfSize:20];
//占位符,当输入是消失
textField.placeholder = @"请输入密码";
//密文输入
textField.secureTextEntry = YES;
textField.tag = 100;
//textField的清除button
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//改变键盘的类型
textField.returnKeyType = UIReturnKeySearch;
textField.keyboardType = UIKeyboardTypeNumberPad;
[self.window addSubview:textField];
[textField release];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
view.backgroundColor = [UIColor greenColor];
//弹出一个自定义视图,默认是键盘
textField.inputView = view;
//给键盘添加一个辅助视图
textField.inputAccessoryView = view;
//给textfield设置代理人
textField.delegate = self;
根据User_login修改UserLoginController类, <Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleRegister" prefHeight="40.0" prefWidth="100.0" text="注册" />中,The controller 'UserLoginController' has no event slot 'handleRegister' user_login:<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="opera.UserLoginController"> <Label layoutX="350.0" layoutY="100.0" text="用户登录"> <Font size="24.0" /> </Label> <Label layoutX="250.0" layoutY="180.0" text="用户名:" /> <TextField fx:id="usernameField" layoutX="330.0" layoutY="180.0" prefWidth="200.0" /> <Label layoutX="250.0" layoutY="230.0" text="密码:" /> <PasswordField fx:id="passwordField" layoutX="330.0" layoutY="230.0" prefWidth="200.0" /> <Button layoutX="330.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleUserLogin" prefHeight="40.0" prefWidth="100.0" text="登录" /> <Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleRegister" prefHeight="40.0" prefWidth="100.0" text="注册" /> <Button layoutX="330.0" layoutY="340.0" mnemonicParsing="false" onAction="#handleBackToStart" prefHeight="40.0" prefWidth="220.0" text="返回主界面" /> </AnchorPane> UserLoginController:package opera; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import java.io.IOException; public class UserLoginController extends Controller { @FXML private TextField usernameField; @FXML private PasswordField passwordField; @FXML public void handleUserLogin(ActionEvent event) { try { String username = usernameField.getText(); String password = passwordField.getText(); if (username.isEmpty() || password.isEmpty()) { showErrorAlert("用户名和密码不能为空"); return; } // 使用硬编码验证(实际项目中应替换为数据库查询) if ("user".equals(username) && "pass".equals(password)) { switchScene("/view/user_dashboard.fxml"); } else { showErrorAlert("用户认证失败:用户名或密码错误"); } } catch (IOException e) { showErrorAlert("场景切换失败:" + e.getMessage()); } catch (Exception e) { showErrorAlert("系统错误:" + e.getMessage()); } } @FXML public void handleBackToStart(ActionEvent event) { try { switchScene("/view/start.fxml"); } catch (IOException e) { showErrorAlert("返回主界面失败:" + e.getMessage()); } } }
最新发布
06-13
根据manager_login.fxml修改ManagerLoginController类,manager_login中<Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleBack" prefHeight="40.0" prefWidth="100.0" text="返回" />,The controller 'ManagerLoginController' has no event slot 'handleBack' manager_login代码:<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="opera.ManagerLoginController"> <Label layoutX="350.0" layoutY="100.0" text="管理员登录"> <Font size="24.0" /> </Label> <Label layoutX="250.0" layoutY="180.0" text="管理员ID:" /> <TextField fx:id="managerIdField" layoutX="330.0" layoutY="180.0" prefWidth="200.0" /> <Label layoutX="250.0" layoutY="230.0" text="密码:" /> <PasswordField fx:id="managerPasswordField" layoutX="330.0" layoutY="230.0" prefWidth="200.0" /> <Button layoutX="330.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleManagerLogin" prefHeight="40.0" prefWidth="100.0" text="登录" /> <Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleBack" prefHeight="40.0" prefWidth="100.0" text="返回" /> </AnchorPane> ManagerLoginController类:package opera; import javafx.event.ActionEvent; // 添加必要的导入 import javafx.fxml.FXML; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import java.io.IOException; // 添加IOException导入 public class ManagerLoginController extends Controller { @FXML private TextField managerIdField; @FXML private PasswordField managerPasswordField; @FXML public void handleManagerLogin(ActionEvent event) { // 改为public访问权限 try { String managerId = managerIdField.getText().trim(); String password = managerPasswordField.getText().trim(); // 空字段检查 if (managerId.isEmpty() || password.isEmpty()) { showErrorAlert("管理员ID和密码不能为空"); return; } // 硬编码验证(实际项目中应使用数据库) if (validateCredentials(managerId, password)) { switchScene("/view/manager_dashboard.fxml"); } else { showErrorAlert("管理员ID或密码错误"); } } catch (IOException e) { // 已导入IOException showErrorAlert("界面切换失败: " + e.getMessage()); } catch (Exception e) { showErrorAlert("系统错误: " + e.getMessage()); } } // 简单的硬编码验证方法 private boolean validateCredentials(String id, String password) { // 实际开发中应替换为数据库查询 return "admin".equals(id) && "admin123".equals(password); } }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值