前言
本周仿写了学生管理系统,这个demo比较简单,下面就做个总结吧。
登录、注册页面
这个界面和先前仿写的3G share的界面较为相似,详见iOS——3GShare总结
添加页面
这个页面的添加需要满足可以添加重名但不同班的学生,限制班级为1班、2班、3班,限制成绩为0~100分。
部分实现代码如下:
if ([self.nameTextField.text isEqualToString:@""] || [self.classTextField.text isEqualToString:@""] || [self.scoreTextField.text isEqualToString:@""]) {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"警告" message:@"输入不能为空,请重新输入!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.scoreTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
BOOL select02 = NO;
NSInteger intValue = [self.scoreTextField.text integerValue];
NSLog(@"成绩为:%ld", intValue);
if (intValue >=0 && intValue <= 100) {
NSLog(@"11111");
select02 = YES;
}
if (select02 == NO) {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"成绩输入格式错误,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.scoreTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
} else {
NSString* nameStr = [NSString stringWithString:self.nameTextField.text];
NSString* classStr = [NSString stringWithString:self.classTextField.text];
NSString* scoreStr = [NSString stringWithString:self.scoreTextField.text];
BOOL nameSelect = YES;
BOOL classSelect = YES;
int k = 0;
for (int i = 0; i < self.nameArr01.count; i++) {
if ([nameStr isEqualToString:self.nameArr01[i]]) {
int j = i;
if ([classStr isEqualToString:self.classArr01[j]]) {
NSLog(@"%@ %@", classStr , self.classArr01[j]);
classSelect = NO;
k = 1;
break;
}
}
}
if (nameSelect && classSelect) {
[self.nameArr01 addObject:nameStr];
[self.classArr01 addObject:classStr];
[self.scoreArr01 addObject:scoreStr];
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.delegate returnNameArr:self.nameArr01 ClassArr:self.classArr01 ScoreArr:self.scoreArr01];
[self.tableView01 reloadData];
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.scoreTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
} else {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加失败,该班有重名,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.scoreTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
}
删除页面
这个页面要实现能删除指定班级指定姓名的同学。
实现代码如下:
BOOL nameSelect = NO;
BOOL classSelect = NO;
int j = 10000;
for (int i = 0; i < self.nameArr02.count; i++) {
if ([nameStr isEqualToString:self.nameArr02[i]]) {
nameSelect = YES;
j = i;
if ([classStr isEqualToString:self.classArr02[j]]) {
classSelect = YES;
break;
}
}
}
if (nameSelect && classSelect) {
[self.nameArr02 removeObjectAtIndex:j];
[self.classArr02 removeObjectAtIndex:j];
[self.scoreArr02 removeObjectAtIndex:j];
j = 10000;
NSLog(@"%lu", self.nameArr02.count);
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.delegate returnNameArr02:self.nameArr02 ClassArr:self.classArr02 ScoreArr:self.scoreArr02];
[self.tableView02 reloadData];
self.nameTextField.text = @"";
self.classTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
} else {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除失败,未找到该生信息,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
self.classTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
}
查找页面
这个页面要实现能查找所有同名的学生,我的解决办法是将所有同名的同学用一个数组存起来,再通过遍历数组来显示数据。
BOOL nameSelect = NO;
NSMutableArray* array01 = [[NSMutableArray alloc] init];
NSMutableArray* array02 = [[NSMutableArray alloc] init];
NSMutableArray* array03 = [[NSMutableArray alloc] init];
for (int i = 0; i < self.nameArr03.count; i++) {
if ([nameStr isEqualToString:self.nameArr03[i]]) {
[array01 addObject:self.nameArr03[i]];
[array02 addObject:self.classArr03[i]];
[array03 addObject:self.scoreArr03[i]];
nameSelect = YES;
}
}
if (nameSelect) {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"成功找到该生信息" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
SubfindViewController* subFindViewController = [[SubfindViewController alloc] init];
subFindViewController.nameArr033 = array01;
subFindViewController.classArr033 = array02;
subFindViewController.scoreArr033 = array03;
[self presentViewController:subFindViewController animated:YES completion:nil];
}];
self.nameTextField.text = @"";
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
} else {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"未找到该生信息,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
修改页面
这个页面主要实现能够修改指定名字和制定班级的同学,如果要修改的班级有重名的同学要能够判断出来,修改的成绩也要限制在0~100分之间。
对于判断修改的班级有重名的同学,我的解决办法是,将所有重名的同学的班级用一个数组保存下来,并记录下数组的元素个数,如果数组的个数大于1就遍历该数组,将要修改的班级的值与数组中的值进行比较,如果有相同的则返回NO。
实现代码如下:
NSString* nameStr = [NSString stringWithString:self.nameTextField.text];
NSString* classStr = [NSString stringWithString:self.classTextField.text];
NSString* classStr02 = [NSString stringWithString:self.classTextField02.text];
NSMutableArray* classArray = [[NSMutableArray alloc] init];
BOOL nameSelect = NO;
BOOL classSelect = NO;
BOOL classSelect02 = YES;
int j = 10000;
int m = 10000;
for (int i = 0; i < self.nameArr04.count; i++) {
if ([nameStr isEqualToString:self.nameArr04[i]]) {
nameSelect = YES;
j = i;
[classArray addObject:self.classArr04[j]];
if ([classStr isEqualToString:self.classArr04[j]]) {
classSelect = YES;
m = j;
}
}
}
if(classArray.count > 1) {
for (int k = 0; k < classArray.count; k++) {
if ([classStr02 isEqualToString:classArray[k]]) {
classSelect02 = NO;
}
}
}
if (nameSelect && classSelect && classSelect02) {
[self.classArr04 replaceObjectAtIndex:m withObject:self.classTextField02.text];
[self.scoreArr04 replaceObjectAtIndex:m withObject:self.scoreTextField.text];
j = 10000;
m = 10000;
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.delegate returnNameArr04:self.nameArr04 ClassArr:self.classArr04 ScoreArr:self.scoreArr04];
[self.tableView04 reloadData];
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.classTextField02.text = @"";
self.scoreTextField.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
} else {
if (nameSelect == NO || classSelect == NO) {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改失败,未找到该生信息,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.classTextField02.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
if (classSelect02 == NO) {
self.alertViewController = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改失败,该班有重名,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* alterAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
self.nameTextField.text = @"";
self.classTextField.text = @"";
self.classTextField02.text = @"";
}];
[self.alertViewController addAction:alterAction];
[self presentViewController:self.alertViewController animated:YES completion:nil];
}
}
}
排序页面
对于排序我用的是冒泡排序,具体时间比较简单,下面是部分代码:
for (int i = 0; i < self.scoreArr05.count - 1; i++) {
for (int j = 0; j < self.scoreArr05.count - i - 1; j++) {
if ([self.scoreArr05[j] intValue] > [self.scoreArr05[j + 1] intValue]) {
NSString* tmp1 = @"";
NSString* tmp2 = @"";
NSString* tmp3 = @"";
tmp1 = [NSString stringWithString: self.nameArr05[j]];
tmp2 = [NSString stringWithString: self.classArr05[j]];
tmp3 = [NSString stringWithString: self.scoreArr05[j]];
self.nameArr05[j] = [NSString stringWithString: self.nameArr05[j + 1]];
self.classArr05[j] = [NSString stringWithString: self.classArr05[j + 1]];
self.scoreArr05[j] = [NSString stringWithString: self.scoreArr05[j + 1]];
self.nameArr05[j + 1] = [NSString stringWithString: tmp1];
self.classArr05[j + 1] = [NSString stringWithString: tmp2];
self.scoreArr05[j + 1] = [NSString stringWithString: tmp3];
}
}
}
总结
对于这个demo,最重要的就是属性传值和协议传值。