iOS学习
- 前言
- 账号界面
- 主页
- 添加学生信息:
- 修改学生信息:
- 删除学生信息:
- 学生信息排序:
- 查找学生信息
- 总结
前言
学生管理系统的仿写较为简单,与c语言的学生管理系统逻辑上相差不大。
账号界面
账号界面与3G share的相同,此处不过多介绍。暑期第二周—3G share
主页
该页面有四个功能,增删改加上排序。设计思路就是点击按钮时,移除视图上所有按钮,加入相应的文本框和确认返回按钮。
添加学生信息:
效果:
主体思路就是判断班级是否存在,学生信息是否重复,是否有非法输入。
-(void)press_sure_add:(UIButton*)btn;
{
if (btn.tag == 101) {
NSString *str_name = self.tf_name.text;
NSString *str_goal = self.tf_goal.text;
NSString *str_class = self.tf_class.text;
int bool1 = 0;
int bool2 = 0;
int bool3 = 0;
if (str_goal.integerValue > 100 || str_goal.integerValue < 0) {
bool3 = 1;
}
for (int i = 0; i < _array_name.count; i++) {
if ([str_name isEqualToString:_array_name[i]] && [str_class isEqualToString:_array_class[i]]) {
bool1 = 1;
bool2 = 1;
break;
}
}
if (![str_class isEqualToString:@"1班"] && ![str_class isEqualToString:@"2班"] && ![str_class isEqualToString:@"3班"]) {
bool2 = 1;
}
if (str_goal.length > 0 && str_name.length > 0 && str_class.length > 0){
if (bool3 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"添加错误" message:@"成绩超出范围0-100" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool1 == 1 && bool2 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"添加错误" message:@"该学生已存在" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool2 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"添加错误" message:@"没有该班级" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else {
[self.btn_back removeFromSuperview];
[self.btn_sure removeFromSuperview];
[self.tf_goal removeFromSuperview];
[self.tf_class removeFromSuperview];
[self.tf_name removeFromSuperview];
[_array_name addObject:str_name];
[_array_goal addObject:str_goal];
[_array_class addObject:str_class];
[self.tableView reloadData];
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"添加成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
[self.view addSubview:self.btn_exit];
[self.view addSubview:self.btn_sort];
[self.view addSubview:self.btn_add];
[self.view addSubview:self.btn_serch];
[self.view addSubview:self.btn_change];
[self.view addSubview:self.btn_delete];
}
} else {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误!" message:@"输入不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
}
}
}
修改学生信息:
此处则是判断信息是否对的上,通过名字班级原成绩来对应一个学生,修改该学生的成绩。
-(void)press_sure_change:(UIButton*)btn
{
if (btn.tag == 111) {
NSString *str_name = self.tf_name.text;
NSString *str_goal = self.tf_goal.text;
NSString *str_class = self.tf_class.text;
NSString *str_newGoal = self.tf_newgoal.text;
int bool1 = 0;
int bool2 = 0;
int bool3 = 0;
int bool4 = 0;
int bool5 = 0;
int bool6 = 0;
if (str_goal.integerValue > 100 || str_goal.integerValue < 0) {
bool3 = 1;
}
if (str_newGoal.integerValue > 100 || str_newGoal.integerValue < 0) {
bool4 = 1;
}
for (int i = 0; i < _array_name.count; i++) {
if ([str_name isEqualToString:_array_name[i]] && [str_class isEqualToString:_array_class[i]]) {
bool1 = 0;
bool2 = 0;
if (![str_goal isEqual: _array_goal[i]]) {
bool5 = 1;
}
bool6 = 1;
break;
}
if ([str_name isEqualToString:_array_name[i]] && ![str_class isEqualToString:_array_class[i]]){
bool1 = 1;
bool2 = 1;
bool6 = 1;
}
}
if (![str_class isEqualToString:@"1班"] && ![str_class isEqualToString:@"2班"] && ![str_class isEqualToString:@"3班"]) {
bool2 = 1;
}
if (str_goal.length > 0 && str_name.length > 0 && str_class.length > 0 && str_newGoal.length > 0){
if (bool6 == 0) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"未找到该学生" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
}
if (bool3 == 1 ) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"成绩超出范围0-100" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool1 == 1 && bool2 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"该学生姓名班级不匹配" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool2 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"没有该班级" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool4 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"新成绩超出范围0-100" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else if (bool5 == 1) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"学生原成绩不匹配" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
}
else {
for (int i = 0; i < _array_name.count; i++) {
if ([str_name isEqualToString:_array_name[i]] && [str_class isEqualToString:_array_class[i]]){
_array_goal[i] = str_newGoal;
}
}
[self.tableView reloadData];
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"修改成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
}
} else {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"错误" message:@"输入不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
}
}
}
删除学生信息:
此处则是判断学生名字班级是否匹配,记录位置在数组中删去即可,故不给出代码了。
学生信息排序:
此处排序与C语言相同,可以通过冒泡排序等排序方法实现
-(void)press_sort{
for (int i = 0 ; i < self.array_name.count - 1; i++) {
for (int j = i + 1; j < self.array_name.count; j ++) {
if ([self.array_goal[i] intValue] < [self.array_goal[j] intValue]) {
id temp = self.array_goal[i];
self.array_goal[i] = self.array_goal[j];
self.array_goal[j] = temp;
temp = self.array_class[i];
self.array_class[i] = self.array_class[j];
self.array_class[j] = temp;
temp = self.array_name[i];
self.array_name[i] = self.array_name[j];
self.array_name[j] = temp;
}
}
}
[self.tableView reloadData];
}
查找学生信息
通过学生的姓名查找学生成绩和班级,该管理系允许不同班同名,故会显示所有该名字的学生。可以使用属性传值的方法来实现传入原数组,再将原数组中匹配的加入到查找数组中。
代码实现:
-(void)press_sure
{
NSString *str_name = self.tf_name.text;
for (int i = 0; i < self.array_name.count; i ++) {
if ([str_name isEqualToString:self.array_name[i]]) {
[self.array_Findgoal addObject:self.array_goal[i]];
[self.array_Findname addObject:self.array_name[i]];
[self.array_Findclass addObject:self.array_class[i]];
self.count_name++;
}
}
if (self.count_name == 0) {
UIAlertController *alertVier = [UIAlertController alertControllerWithTitle:@"查找失败!" message:@"未查找到该学生" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[alertVier addAction:action01];
[self presentViewController:alertVier animated:YES completion:nil];
} else {
self.count_name = 0;
[self.tableView reloadData];
}
}
总结
学生管理系统的仿写较为简单,还有难点在于文本框的限制,需要对正则表达式进行学习。