本文共 50951 字,大约阅读时间需要 169 分钟。
因为界面比较多,为了方便理解,效果图就放在各段代码的后面
GitHub地址:开始界面的设置(根视图的修改):
在AppDelegate.m 中修改方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyWindow]; ShowViewController *root = [[ShowViewController alloc] init]; self.window.rootViewController = root; return YES;}
然后在 ShowViewController.m 中:
#import "ShowViewController.h"#import "LoadViewController.h"@interface ShowViewController ()@end@implementation ShowViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UILabel *label = [[UILabel alloc] init]; [self.view addSubview:label]; label.text = @"学生管理系统"; label.textColor = [UIColor whiteColor]; label.frame = CGRectMake(0, 210, 414, 40); label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:27]; UILabel *label1 = [[UILabel alloc] init]; [self.view addSubview:label1]; label1.text = @"TAY的"; label1.textColor = [UIColor whiteColor]; label1.frame = CGRectMake(0, 150, 414, 40); label1.textAlignment = NSTextAlignmentCenter; label1.font = [UIFont systemFontOfSize:38]; UILabel *label2 = [[UILabel alloc] init]; [self.view addSubview:label2]; label2.text = @"TangAoyang's Student Management System"; label2.numberOfLines = 0; label2.font = [UIFont systemFontOfSize:20]; label2.textColor = [UIColor whiteColor]; label2.frame = CGRectMake(0, 260, [UIScreen mainScreen].bounds.size.width, 30); label2.textAlignment = NSTextAlignmentCenter; UIImage *zyyImage = [UIImage imageNamed:@"zyy.jpg"]; UIImageView *zyyImageView = [[UIImageView alloc] initWithImage:zyyImage]; zyyImageView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, 280); [self.view addSubview:zyyImageView]; self.view.backgroundColor = [UIColor blackColor]; //两秒后执行函数 //P1:需要执行的函数 //P2:执行对象 //P3:时间(单位是秒) [self performSelector:@selector(next) withObject:self afterDelay:2];}- (void)next { LoadViewController *root = [[LoadViewController alloc] init]; self.view.window.rootViewController = root; }
效果:(两秒后跳转到登陆界面)
正式进来后首先是登陆界面
在LoadViewController.h 中:#import#import "RegisterViewController.h"NS_ASSUME_NONNULL_BEGIN@interface LoadViewController : UIViewController @property UITextField *userTextField;@property UITextField *passTextField;@property UIButton *loadButton;@property UIButton *registerButton;@end
在 LoadViewController.m 中:
#import "LoadViewController.h"#import "RegisterViewController.h"#import "FirstViewController.h"@interface LoadViewController (){ NSString *userStr; NSString *passStr; NSMutableArray *userArr; NSMutableArray *passArr;}@end@implementation LoadViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *backgroundImage = [UIImage imageNamed:@"背景7.jpg"]; UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage]; backgroundImageView.frame = self.view.bounds; [self.view insertSubview:backgroundImageView atIndex:0]; UIImage *lineImage = [UIImage imageNamed:@"竖线.png"]; UIImageView *lineImageView = [[UIImageView alloc] initWithImage:lineImage]; lineImageView.frame = CGRectMake(45, 10, 10, 30); UIImage *userImage = [UIImage imageNamed:@"用户.png"]; UIImageView *userImageView = [[UIImageView alloc] initWithImage:userImage]; userImageView.frame = CGRectMake(0, 0, 45, 45); UIView *user_View = [[UIView alloc] init]; user_View.frame = CGRectMake(0, 0, 55, 50); [user_View addSubview:userImageView]; [user_View addSubview:lineImageView]; _userTextField = [[UITextField alloc] init]; _userTextField.placeholder = @"请输入账号"; _userTextField.textColor = [UIColor whiteColor]; _userTextField.tintColor = [UIColor whiteColor]; _userTextField.borderStyle = UITextBorderStyleLine; _userTextField.layer.masksToBounds = YES; _userTextField.layer.cornerRadius = 5; _userTextField.layer.borderWidth = 2; _userTextField.layer.borderColor = [UIColor whiteColor].CGColor; _userTextField.backgroundColor = [UIColor clearColor]; [self.view addSubview:_userTextField]; _userTextField.delegate = self; _userTextField.frame = CGRectMake(50, 370, 320, 55); _userTextField.keyboardType = UIKeyboardTypeDefault; _userTextField.leftViewMode = UITextFieldViewModeAlways; _userTextField.leftView = user_View; UIImageView *lineImageView2 = [[UIImageView alloc] initWithImage:lineImage]; lineImageView2.frame = CGRectMake(45, 10, 10, 30); UIImage *passImage = [UIImage imageNamed:@"密码.png"]; UIImageView *passImageView = [[UIImageView alloc] initWithImage:passImage]; passImageView.frame = CGRectMake(0, 0, 45, 45); UIView *pass_View = [[UIView alloc] init]; pass_View.frame = CGRectMake(0, 0, 55, 50); [pass_View addSubview:passImageView]; [pass_View addSubview:lineImageView2]; _passTextField = [[UITextField alloc] init]; _passTextField.tintColor = [UIColor whiteColor]; _passTextField.textColor = [UIColor whiteColor]; _passTextField.placeholder = @"请输入密码"; _passTextField.borderStyle = UITextBorderStyleRoundedRect; _passTextField.layer.masksToBounds = YES; _passTextField.layer.cornerRadius = 5; _passTextField.layer.borderWidth = 2; _passTextField.layer.borderColor = [UIColor whiteColor].CGColor; [self.view addSubview:_passTextField]; _passTextField.backgroundColor = [UIColor clearColor]; _passTextField.delegate = self; _passTextField.frame = CGRectMake(50, 450, 320, 55); _passTextField.keyboardType = UIKeyboardTypeDefault; _passTextField.secureTextEntry = YES; _passTextField.leftViewMode = UITextFieldViewModeAlways; _passTextField.leftView = pass_View; _loadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _loadButton.frame = CGRectMake(80, 525, 100, 35); _loadButton.titleLabel.font = [UIFont systemFontOfSize:23]; [_loadButton setTitle:@"登陆" forState:UIControlStateNormal]; [_loadButton addTarget:self action:@selector(pressLoad) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:_loadButton]; _loadButton.tintColor = [UIColor whiteColor]; _registerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:_registerButton]; _registerButton.frame = CGRectMake(235, 525, 100, 35); _registerButton.titleLabel.font = [UIFont systemFontOfSize:23]; _registerButton.tintColor = [UIColor whiteColor]; [_registerButton setTitle:@"注册" forState:UIControlStateNormal]; [_registerButton addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchDown]; userArr = [[NSMutableArray alloc] init]; [userArr addObject:@"111"]; [userArr addObject:@"222"]; [userArr addObject:@"333"]; [userArr addObject:@"444"]; passArr = [[NSMutableArray alloc] init]; [passArr addObject:@"aaa"]; [passArr addObject:@"sss"]; [passArr addObject:@"ddd"]; [passArr addObject:@"fff"]; }- (void)pressLoad{ int i ; for(i = 0; i < userArr.count; i++) { if([_userTextField.text isEqualToString:userArr[i]] && [_passTextField.text isEqualToString:passArr[i]]) { FirstViewController *firstRoot = [[FirstViewController alloc] init]; self.view.window.rootViewController = firstRoot; } } if(i == userArr.count){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"账号与密码不匹配!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:sureAction]; [self presentViewController:alert animated:NO completion:nil]; }}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES];}- (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField == _userTextField) { [_passTextField becomeFirstResponder]; } if(textField == _passTextField) { [textField resignFirstResponder]; } return YES; }- (void)pressRegister{ RegisterViewController *registerViewController = [[RegisterViewController alloc] init]; //反传的代理 registerViewController.regiserViewControllerDelegate = self; //利用属性正传 registerViewController.RuserArr = userArr; registerViewController.RpassArr = passArr; [self presentViewController:registerViewController animated:NO completion:nil]; }//协议方法- (void)pass1:(NSString *)str1 pass2:(NSString *)str2 { _userTextField.text = str1; _passTextField.text = str2; }
登陆界面显示效果图:
密码与账号不匹配的警告: 下来是注册界面(注册完后返回登陆在登陆框上显示,如果有重复的账号显示提示框,并且未成功注册) 在 RegisterViewController.h 中:#importNS_ASSUME_NONNULL_BEGIN@protocol RegisterViewControllerDelegate - (void)pass1:(NSString *)str1 pass2:(NSString *)str2;@end@interface RegisterViewController : UIViewController @property (nonatomic, weak) id regiserViewControllerDelegate;@property UITextField *messTextField;@property UITextField *userTextField;@property UITextField *passTextField;@property NSMutableArray *RuserArr;@property NSMutableArray *RpassArr;@end
在 RegisterViewController.m 中:
#import "RegisterViewController.h"@interface RegisterViewController (){ NSArray *numArr; NSArray *nameArr;}@end@implementation RegisterViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *backgroundImage = [UIImage imageNamed:@"背景7.jpg"]; UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage]; backgroundImageView.frame = self.view.bounds; [self.view insertSubview:backgroundImageView atIndex:0]; UIImage *messImage = [UIImage imageNamed:@"邮箱.png"]; UIImageView *messImageView = [[UIImageView alloc] initWithImage:messImage]; messImageView.frame = CGRectMake(5, 0, 40, 40); UIImage *lineImage = [UIImage imageNamed:@"竖线.png"]; UIImageView *lineImageView = [[UIImageView alloc] initWithImage:lineImage]; lineImageView.frame = CGRectMake(45, 10, 10, 30); UIView *mess_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 50)]; [mess_View addSubview:lineImageView]; [mess_View addSubview:messImageView]; _messTextField = [[UITextField alloc] init]; [self.view addSubview:_messTextField]; _messTextField.delegate = self; _messTextField.tintColor = [UIColor whiteColor]; _messTextField.textColor = [UIColor whiteColor]; _messTextField.placeholder = @"请输入你的邮箱"; _messTextField.layer.masksToBounds = YES; _messTextField.layer.cornerRadius = 5; _messTextField.layer.borderWidth = 2; _messTextField.frame = CGRectMake(50, 300, 300, 55); _messTextField.borderStyle = UITextBorderStyleRoundedRect; _messTextField.layer.borderColor = [UIColor whiteColor].CGColor; _messTextField.backgroundColor = [UIColor clearColor]; _messTextField.leftViewMode = UITextFieldViewModeAlways; _messTextField.leftView = mess_View; UIImage *userImage = [UIImage imageNamed:@"用户.png"]; UIImageView *userImageView = [[UIImageView alloc] initWithImage:userImage]; userImageView.frame = CGRectMake(5, 0, 40, 40); UIImageView *lineView2 = [[UIImageView alloc] initWithImage:lineImage]; lineView2.frame = CGRectMake(45, 10, 10, 30); UIView *user_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 50)]; [user_View addSubview:lineView2]; [user_View addSubview:userImageView]; _userTextField = [[UITextField alloc] init]; [self.view addSubview:_userTextField]; _userTextField.delegate = self; _userTextField.tintColor = [UIColor whiteColor]; _userTextField.textColor = [UIColor whiteColor]; _userTextField.placeholder = @"请输入账号"; _userTextField.backgroundColor = [UIColor clearColor]; _userTextField.frame = CGRectMake(50, 375, 300, 55); _userTextField.borderStyle = UITextBorderStyleRoundedRect; _userTextField.layer.masksToBounds = YES; _userTextField.layer.cornerRadius = 5; _userTextField.layer.borderWidth = 2; _userTextField.layer.borderColor = [UIColor whiteColor].CGColor; _userTextField.leftViewMode = UITextFieldViewModeAlways; _userTextField.leftView = user_View; UIImage *passImage = [UIImage imageNamed:@"密码.png"]; UIImageView *passImageView = [[UIImageView alloc] initWithImage:passImage]; passImageView.frame = CGRectMake(5, 0, 40, 40); UIImageView *lineImageView3 = [[UIImageView alloc] initWithImage:lineImage]; lineImageView3.frame = CGRectMake(45, 10, 10, 30); UIView *pass_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 50)]; [pass_View addSubview:passImageView]; [pass_View addSubview:lineImageView3]; _passTextField = [[UITextField alloc] init]; [self.view addSubview:_passTextField]; _passTextField.delegate = self; _passTextField.secureTextEntry = YES; _passTextField.tintColor = [UIColor whiteColor]; _passTextField.textColor = [UIColor whiteColor]; _passTextField.placeholder = @"请输入密码"; _passTextField.keyboardType = UIKeyboardTypeDefault; _passTextField.layer.masksToBounds = YES; _passTextField.layer.cornerRadius = 5; _passTextField.layer.borderWidth = 2; _passTextField.backgroundColor = [UIColor clearColor]; _passTextField.frame = CGRectMake(50, 450, 300, 55); _passTextField.borderStyle = UITextBorderStyleRoundedRect; _passTextField.layer.borderColor = [UIColor whiteColor].CGColor; _passTextField.leftViewMode = UITextFieldViewModeAlways; _passTextField.leftView = pass_View; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:button]; [button setTitle:@"返回登陆" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:23]; button.tintColor = [UIColor whiteColor]; button.layer.backgroundColor = [UIColor clearColor].CGColor; button.frame = CGRectMake(195, 530, 100, 50); [button addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchDown]; UIButton *Rbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:Rbutton]; [Rbutton setTitle:@"注册" forState:UIControlStateNormal]; Rbutton.titleLabel.font = [UIFont systemFontOfSize:23]; Rbutton.tintColor = [UIColor whiteColor]; Rbutton.layer.backgroundColor = [UIColor clearColor].CGColor; Rbutton.frame = CGRectMake(85, 530, 100, 50); [Rbutton addTarget:self action:@selector(pressRBtn) forControlEvents:UIControlEventTouchDown]; }- (void)pressBtn{ [self.view endEditing:YES]; [self dismissViewControllerAnimated:NO completion:nil]; //代理返回值 if([_regiserViewControllerDelegate respondsToSelector:@selector(pass1:pass2:)]) { [_regiserViewControllerDelegate pass1:_userTextField.text pass2:_passTextField.text]; } }- (void)pressRBtn{ int i; //查重 for(i = 0; i < _RuserArr.count; i++){ if([_userTextField.text isEqualToString:_RuserArr[i]]){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"账号重复!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:sure]; [self presentViewController:alert animated:NO completion:nil]; break; } } //如果没有重复 if(i == _RuserArr.count){ UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功,请重新登录!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; [_RuserArr addObject:_userTextField.text]; [_RpassArr addObject:_passTextField.text]; }}//按return键进入下一个textField- (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField == _messTextField) { [_userTextField becomeFirstResponder]; } if(textField == _userTextField){ [_passTextField becomeFirstResponder]; } if(textField == _passTextField){ [textField resignFirstResponder]; } return YES; }//点击空白处键盘回收- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event { [_messTextField endEditing:YES]; [_passTextField endEditing:YES]; [_userTextField endEditing:YES]; }
注册效果图:
输入重复账号的警告: 下来就要正式进入app啦! 在这里,我定义一个储存信息的类 Student 来存学生信息(为了方便起见) 在 Student.h 中:#importNS_ASSUME_NONNULL_BEGIN@interface Student : NSObject@property NSString *nameStr;@property NSString *classStr;@property NSString *ageStr;@end
增删改都自定义了协议(为了反传值),查不需要传值,故没有协议
进入后首页: 在 FirstViewController.h 中:#import#import "AddViewController.h"#import "DeleteViewController.h"#import "ChangeViewController.h"#import "Student.h"NS_ASSUME_NONNULL_BEGIN@class Student;@interface FirstViewController : UIViewController @property UITableView *tableView;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property NSMutableArray *stuArr;@end
在 FirstViewController.m 中:
#import "FirstViewController.h"#import "FirstTableViewCell.h"#import "AddViewController.h"#import "DeleteViewController.h"#import "ChangeViewController.h"#import "FindViewController.h"#import "Student.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *backImage = [UIImage imageNamed:@"背景10.jpg"]; UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage]; backImageView.frame = self.view.bounds; [self.view insertSubview:backImageView atIndex:0]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 500) style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.backgroundColor = [UIColor clearColor]; _tableView.tableFooterView = [[UIView alloc] init]; [_tableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"111"]; UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:addButton]; addButton.frame = CGRectMake(100, 570, 100, 50); [addButton setTitle:@"add" forState:UIControlStateNormal]; addButton.tintColor = [UIColor whiteColor]; addButton.titleLabel.font = [UIFont systemFontOfSize:30]; [addButton addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchDown]; UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:deleteButton]; deleteButton.frame = CGRectMake(250, 570, 100, 50); [deleteButton setTitle:@"delete" forState:UIControlStateNormal]; deleteButton.tintColor = [UIColor whiteColor]; deleteButton.titleLabel.font = [UIFont systemFontOfSize:30]; [deleteButton addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchDown]; UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:changeButton]; changeButton.frame = CGRectMake(100, 640, 100, 50); [changeButton setTitle:@"change" forState:UIControlStateNormal]; changeButton.tintColor = [UIColor whiteColor]; changeButton.titleLabel.font = [UIFont systemFontOfSize:30]; [changeButton addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchDown]; UIButton *findButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:findButton]; findButton.frame = CGRectMake(250, 640, 100, 50); [findButton setTitle:@"find" forState:UIControlStateNormal]; findButton.tintColor = [UIColor whiteColor]; findButton.titleLabel.font = [UIFont systemFontOfSize:30]; [findButton addTarget:self action:@selector(find) forControlEvents:UIControlEventTouchDown]; UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:exitButton]; exitButton.frame = CGRectMake(250, 730, 100, 50); [exitButton setTitle:@"exit" forState:UIControlStateNormal]; exitButton.tintColor = [UIColor whiteColor]; exitButton.titleLabel.font = [UIFont systemFontOfSize:30]; [exitButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchDown]; _nameArr = [[NSMutableArray alloc] init]; [_nameArr addObject:@"张三"]; [_nameArr addObject:@"李四"]; [_nameArr addObject:@"王麻子"]; [_nameArr addObject:@"hahaha"]; _ageArr = [[NSMutableArray alloc ]init]; [_ageArr addObject:@"38"]; [_ageArr addObject:@"250"]; [_ageArr addObject:@"2"]; [_ageArr addObject:@"123"]; _classArr = [[NSMutableArray alloc] init]; [_classArr addObject:@"a班"]; [_classArr addObject:@"b班"]; [_classArr addObject:@"c班"]; [_classArr addObject:@"d班"]; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath]; [cell give:indexPath Arr1:_nameArr Arr2:_ageArr Arr3:_classArr]; return cell; }//退出- (void)exit{ exit(0);}//tableView的行数根据学生数目来设- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _nameArr.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70;}//增加的点击事件//这四个方法基本相同- (void)add { AddViewController *addRoot = [[AddViewController alloc] init]; //正传 addRoot.nameArr = _nameArr; addRoot.ageArr = _ageArr; addRoot.classArr = _classArr; //反传的代理 addRoot.addDelegate = self; [self presentViewController:addRoot animated:NO completion:nil]; }//删除的点击事件- (void)delete { DeleteViewController *deleteRoot = [[DeleteViewController alloc] init]; deleteRoot.nameArr = _nameArr; deleteRoot.ageArr = _ageArr; deleteRoot.classArr = _classArr; deleteRoot.deleteDelegate = self; [self presentViewController:deleteRoot animated:NO completion:nil]; }//修改的点击事件- (void)change{ ChangeViewController *changeRoot = [[ChangeViewController alloc] init]; changeRoot.nameArr = _nameArr; changeRoot.ageArr = _ageArr; changeRoot.classArr = _classArr; changeRoot.changeDelegate = self; [self presentViewController:changeRoot animated:NO completion:nil]; }//查找的点击事件- (void)find{ FindViewController *findRoot = [[FindViewController alloc] init]; findRoot.nameArr = _nameArr; findRoot.ageArr = _ageArr; findRoot.classArr = _classArr; [self presentViewController:findRoot animated:NO completion:nil]; }//增加的代理方法- (void)pass:(Student *)student { [_nameArr addObject:student.nameStr]; [_ageArr addObject:student.ageStr]; [_classArr addObject:student.classStr]; [_tableView reloadData]; }//删除的代理方法- (void)deletepass1:(NSMutableArray *)nameArr pass2:(NSMutableArray *)ageArr pass3:(NSMutableArray *)classArr{ _nameArr = nameArr; _ageArr = ageArr; _classArr = classArr; [_tableView reloadData]; }//修改的代理方法- (void)changePass1:(NSMutableArray *)nameArr Pass2:(NSMutableArray *)ageArr Pass3:(NSMutableArray *)classArr{ _nameArr = nameArr; _ageArr = ageArr; _classArr = classArr; [_tableView reloadData]; }//点击单元格不显示- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO];}
tableview的cell:
在 FirstTableViewCell.h 中:#importNS_ASSUME_NONNULL_BEGIN@interface FirstTableViewCell : UITableViewCell@property UILabel *nameLabel;@property UILabel *classLabel;@property UILabel *ageLabel;- (void)give:(NSIndexPath *)indexPath Arr1:(NSMutableArray *)nameArr Arr2:(NSMutableArray *)ageArr Arr3:(NSMutableArray *)classArr;@endNS_ASSUME_NONNULL_END
在 FirstTableViewCell.m 中:
#import "FirstTableViewCell.h"@implementation FirstTableViewCell//为了方便,代码封装- (void)give:(NSIndexPath *)indexPath Arr1:(NSMutableArray *)nameArr Arr2:(NSMutableArray *)ageArr Arr3:(NSMutableArray *)classArr { self.nameLabel.text = nameArr[indexPath.row]; self.ageLabel.text = ageArr[indexPath.row]; self.classLabel.text = classArr[indexPath.row]; self.backgroundColor = [UIColor clearColor];}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; _nameLabel = [[UILabel alloc] init]; [self.contentView addSubview:_nameLabel]; _classLabel = [[UILabel alloc] init]; [self.contentView addSubview:_classLabel]; _ageLabel = [[UILabel alloc] init]; [self.contentView addSubview:_ageLabel]; return self; }- (void)layoutSubviews { [super layoutSubviews]; _nameLabel.frame = CGRectMake(5, 25, 100, 20); _nameLabel.font = [UIFont systemFontOfSize:20]; _nameLabel.textAlignment = NSTextAlignmentCenter; _nameLabel.textColor = [UIColor whiteColor]; _classLabel.frame = CGRectMake(120, 25, 150, 20); _classLabel.font = [UIFont systemFontOfSize:20]; _classLabel.textAlignment = NSTextAlignmentCenter; _classLabel.textColor = [UIColor whiteColor]; _ageLabel.frame = CGRectMake(300, 25, 50, 20); _ageLabel.font = [UIFont systemFontOfSize:20]; _ageLabel.textAlignment = NSTextAlignmentCenter; _ageLabel.textColor = [UIColor whiteColor]; }- (void)awakeFromNib { [super awakeFromNib]; // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}
效果图:
增加:(具有查重,并且输入不能为空) 在 AddViewController.h 中:#import#import "Student.h"NS_ASSUME_NONNULL_BEGIN@class Student;@protocol addDelegate - (void)pass: (Student *)student;@end@interface AddViewController : UIViewController @property UITextField *nameTextField;@property UITextField *classTextField;@property UITextField *ageTextField;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property UITableView *tableView;@property id addDelegate;@end
在 AddViewController.m 中:
#import "AddViewController.h"#import "FirstTableViewCell.h"@interface AddViewController ()@end@implementation AddViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *backImage = [UIImage imageNamed:@"背景13.jpg"]; UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage]; backImageView.frame = self.view.bounds; [self.view insertSubview:backImageView atIndex:0]; UIImage *nameImage = [UIImage imageNamed:@"姓名.png"]; UIImageView *nameImageView = [[UIImageView alloc] initWithImage:nameImage]; nameImageView.frame = CGRectMake(3, 3, 45, 45); _nameTextField = [[UITextField alloc] init]; _nameTextField.layer.masksToBounds = YES; _nameTextField.layer.cornerRadius = 5; _nameTextField.layer.borderWidth = 2; _nameTextField.layer.borderColor = [UIColor whiteColor].CGColor; _nameTextField.tintColor = [UIColor whiteColor]; _nameTextField.textColor = [UIColor whiteColor]; _nameTextField.backgroundColor = [UIColor clearColor]; _nameTextField.frame = CGRectMake(80, 500, 250, 50); _nameTextField.placeholder = @"请输入学生姓名"; _nameTextField.leftViewMode = UITextFieldViewModeAlways; _nameTextField.leftView = nameImageView; [self.view addSubview:_nameTextField]; UIImage *ageImage = [UIImage imageNamed:@"年龄.png"]; UIImageView *ageImageView = [[UIImageView alloc] initWithImage:ageImage]; ageImageView.frame = CGRectMake(3, 3, 45, 45); _ageTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 565, 250, 50)]; [self.view addSubview:_ageTextField]; _ageTextField.placeholder = @"请输入学生年龄"; _ageTextField.layer.masksToBounds = YES; _ageTextField.layer.borderWidth = 2; _ageTextField.layer.cornerRadius = 5; _ageTextField.tintColor = [UIColor whiteColor]; _ageTextField.textColor = [UIColor whiteColor]; _ageTextField.backgroundColor = [UIColor clearColor]; _ageTextField.layer.borderColor = [UIColor whiteColor].CGColor; _ageTextField.leftViewMode = UITextFieldViewModeAlways; _ageTextField.leftView = ageImageView; UIImage *classImage = [UIImage imageNamed:@"房子.png"]; UIImageView *classImageView = [[UIImageView alloc] initWithImage:classImage]; classImageView.frame = CGRectMake(3, 3, 45, 45); _classTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 630, 250, 50)]; [self.view addSubview:_classTextField]; _classTextField.placeholder = @"请输入学生班级"; _classTextField.layer.masksToBounds = YES; _classTextField.layer.borderWidth = 2; _classTextField.layer.cornerRadius = 5; _classTextField.tintColor = [UIColor whiteColor]; _classTextField.textColor = [UIColor whiteColor]; _classTextField.backgroundColor = [UIColor clearColor]; _classTextField.layer.borderColor = [UIColor whiteColor].CGColor; _classTextField.leftViewMode = UITextFieldViewModeAlways; _classTextField.leftView = classImageView; UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:add]; add.frame = CGRectMake(110, 700, 80, 30); add.titleLabel.font = [UIFont systemFontOfSize:25]; [add setTitle:@"完成" forState:UIControlStateNormal]; add.backgroundColor = [UIColor clearColor]; add.tintColor = [UIColor whiteColor]; [add addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchDown]; UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:back]; back.frame = CGRectMake(250, 700, 80, 30); back.titleLabel.font = [UIFont systemFontOfSize:25]; [back setTitle:@"返回" forState:UIControlStateNormal]; back.backgroundColor = [UIColor clearColor]; back.tintColor = [UIColor blackColor]; [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchDown]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2 - 100) style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.backgroundColor = [UIColor clearColor]; _tableView.tableFooterView = [[UIView alloc] init]; [_tableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"111"]; //键盘上移 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath]; [cell give:indexPath Arr1:_nameArr Arr2:_ageArr Arr3:_classArr]; return cell; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _nameArr.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70;}- (void)add{ //判断输入是否有空的 if(_ageTextField.text.length <= 0 || _nameTextField.text.length <= 0 || _classTextField.text.length <= 0){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入不能为空!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:sure]; [self presentViewController:alert animated:NO completion:nil]; } else { int i; //查重 for(i = 0; i< _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text]){ break; } } if(i != _nameArr.count){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"姓名有重复,请核对后再试!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:sure]; [self presentViewController:alert animated:NO completion:nil]; } if(i == _nameArr.count){ [self dismissViewControllerAnimated:NO completion:nil]; if([_addDelegate respondsToSelector:@selector(pass:)]) { Student *stu = [[Student alloc] init]; stu.nameStr = _nameTextField.text; stu.ageStr = _ageTextField.text; stu.classStr = _classTextField.text; [_addDelegate pass:stu]; } } }}- (void)back { [self dismissViewControllerAnimated:NO completion:nil];}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{ [_nameTextField endEditing:YES]; [_ageTextField endEditing:YES]; [_classTextField endEditing:YES];}- (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField == _nameTextField) { [_ageTextField becomeFirstResponder]; } if(textField == _ageTextField){ [_classTextField becomeFirstResponder]; } if(textField == _classTextField){ [textField resignFirstResponder]; } return YES; }- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO];}-(void)keyboardWillShow : (NSNotification *)notify { CGFloat kbHeight = [[notify.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; CGFloat offset = kbHeight - 100; double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; if (offset > 0) { [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height); }]; }}-(void)keyboardWillHide: (NSNotification *)notify { double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); }];}-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}
增加界面效果图:
查重: 判空: 删除界面:(可判断是否存在该学生) 在 DeleteViewController.h 中:#import#import "Student.h"NS_ASSUME_NONNULL_BEGIN@class Student;@protocol DeleteDelegate - (void)deletepass1:(NSMutableArray *)nameArr pass2:(NSMutableArray *)ageArr pass3:(NSMutableArray *)classArr;@end@interface DeleteViewController : UIViewController @property UITextField *nameTextField;@property UITableView *tableView;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property id deleteDelegate;@end
增删改查大同小异,下面三个我只放执行的核心代码了
在 DeleteViewController.m 中:- (void)delete{ //判断是否存在 for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text]){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已找到此人,确认是否删除" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ [self delete2]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:sure]; [alert addAction:cancel]; [self presentViewController:alert animated:NO completion:nil]; break; } } //不存在 if(i == _nameArr.count) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } //删除操作- (void)delete2{ [_nameArr removeObjectAtIndex:i]; [_ageArr removeObjectAtIndex:i]; [_classArr removeObjectAtIndex:i]; [self dismissViewControllerAnimated:NO completion:nil]; if([_deleteDelegate respondsToSelector:@selector(deletepass1:pass2:pass3:)]){ [_deleteDelegate deletepass1:_nameArr pass2:_ageArr pass3:_classArr]; }}
删除界面:
判断是否存在: 若存在操作: 删除后返回主界面: 修改: 因为修改需要先判断是否存在若存在再弹出输入修改信息的textField,故为了美观,我分为两个界面来写:在 ChangeViewController.h 中:
#import#import "ChangeSonViewController.h"NS_ASSUME_NONNULL_BEGIN@protocol ChangeDelegate - (void)changePass1:(NSMutableArray *)nameArr Pass2:(NSMutableArray *)ageArr Pass3:(NSMutableArray *)classArr;@end@interface ChangeViewController : UIViewController @property UITextField *nameTextField;@property UITableView *tableView;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property id changeDelegate;@end
在 ChangeViewController.m 中:
逻辑依旧是先判断是否存在,然后进行操作- (void)change{ int i; for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text]){ break; } } if(i != _nameArr.count){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"存在此人,是否继续修改" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action){ ChangeSonViewController *sonRoot = [[ChangeSonViewController alloc] init]; sonRoot.nameArr = self->_nameArr; sonRoot.ageArr = self->_ageArr; sonRoot.classArr = self->_classArr; sonRoot.str = self->_nameTextField.text; sonRoot.changeSonDelegate = self; [self presentViewController:sonRoot animated:NO completion:nil]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:cancel]; [alert addAction:sure]; [self presentViewController:alert animated:NO completion:nil]; } if(i == _nameArr.count){ UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; }}
判断有无效果图:
若存在,则跳转下一个界面: 在 ChangeSonViewController.h 中:#importNS_ASSUME_NONNULL_BEGIN@protocol ChangeSonDelegate - (void)changeSonPass1:(NSMutableArray *)nameArr Pass2:(NSMutableArray *)ageArr Pass3:(NSMutableArray *)classArr;@end@interface ChangeSonViewController : UIViewController @property UITextField *nameTextField;@property UITextField *classTextField;@property UITextField *ageTextField;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property NSString *str;@property id changeSonDelegate;@end
在 ChangeSonViewController.m 中:
需要有三个textField- (void)change{ //找到该学生,将修改后的值赋给他 int i; for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_str]){ [_ageArr replaceObjectAtIndex:i withObject:_ageTextField.text]; [_classArr replaceObjectAtIndex:i withObject:_classTextField.text]; } } if([_changeSonDelegate respondsToSelector:@selector(changeSonPass1:Pass2:Pass3:)]){ [_changeSonDelegate changeSonPass1:_nameArr Pass2:_ageArr Pass3:_classArr]; } [self dismissViewControllerAnimated:NO completion:nil];}
效果图:
此处我将李四的信息修改: 点击完成: 修改成功!最后是查找:
同样,因为要将找到的学生单独显示出来,我新建了一个界面,使用跳转在 FindViewController.h 中:
#importNS_ASSUME_NONNULL_BEGIN@interface FindViewController : UIViewController @property UITextField *nameTextField;@property UITableView *tableView;@property UITextField *classTextField;@property UITextField *ageTextField;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@property NSMutableArray *snameArr;@property NSMutableArray *sageArr;@property NSMutableArray *sclassArr;@end
在 FindViewController.m 中:
- (void)find{ _snameArr = [[NSMutableArray alloc] init]; _sageArr = [[NSMutableArray alloc] init]; _sclassArr = [[NSMutableArray alloc] init]; BOOL find = NO; int i = 0; int j; int t; if((_nameTextField.text.length > 0) && (_ageTextField.text.length <= 0) && (_classTextField.text.length <= 0)){ for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text]){ find = YES; [_snameArr addObject:_nameArr[i]]; [_sageArr addObject:_ageArr[i]]; [_sclassArr addObject:_classArr[i]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_ageTextField.text.length > 0) && (_nameTextField.text.length <= 0) && (_classTextField.text.length <= 0)){ for(j = 0; j < _ageArr.count; j++){ if([_ageArr[j] isEqualToString:_ageTextField.text]){ find = YES; [_snameArr addObject:_nameArr[j]]; [_sageArr addObject:_ageArr[j]]; [_sclassArr addObject:_classArr[j]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_classTextField.text.length > 0) && (_ageTextField.text.length <= 0) && (_nameTextField.text.length <= 0)){ for(t = 0; t < _classArr.count; t++){ if([_classArr[t] isEqualToString:_classTextField.text]){ find = YES; [_snameArr addObject:_nameArr[t]]; [_sageArr addObject:_ageArr[t]]; [_sclassArr addObject:_classArr[t]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_nameTextField.text.length > 0) && (_ageTextField.text.length > 0) && (_classTextField.text.length <= 0)){ for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text] && [_ageArr[i] isEqualToString:_ageTextField.text]){ find = YES; [_snameArr addObject:_nameArr[i]]; [_sageArr addObject:_ageArr[i]]; [_sclassArr addObject:_classArr[i]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_nameTextField.text.length > 0) && (_classTextField.text.length > 0) && (_ageTextField.text.length <= 0)){ for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text] && [_classArr[i] isEqualToString:_classTextField.text]){ find = YES; [_snameArr addObject:_nameArr[i]]; [_sageArr addObject:_ageArr[i]]; [_sclassArr addObject:_classArr[i]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_classTextField.text.length > 0) && (_ageTextField.text.length > 0) && (_nameTextField.text.length <= 0)){ for(i = 0; i < _nameArr.count; i++){ if([_classArr[i] isEqualToString:_classTextField.text] && [_ageArr[i] isEqualToString:_ageTextField.text]){ find = YES; [_snameArr addObject:_nameArr[i]]; [_sageArr addObject:_ageArr[i]]; [_sclassArr addObject:_classArr[i]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if((_nameTextField.text.length > 0) && (_classTextField.text.length > 0) && (_ageTextField.text.length > 0)){ for(i = 0; i < _nameArr.count; i++){ if([_nameArr[i] isEqualToString:_nameTextField.text] && [_classArr[i] isEqualToString:_classTextField.text] && [_ageArr[i] isEqualToString:_ageTextField.text]){ find = YES; [_snameArr addObject:_nameArr[i]]; [_sageArr addObject:_ageArr[i]]; [_sclassArr addObject:_classArr[i]]; } } if(find == NO) { UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到此人,请确认是否输入正确" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sure1 = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:nil]; [alert1 addAction:sure1]; [self presentViewController:alert1 animated:NO completion:nil]; } } if(find == YES){ FindSonViewController *sonRoot = [[FindSonViewController alloc] init]; sonRoot.nameArr = _snameArr; sonRoot.ageArr = _sageArr; sonRoot.classArr = _sclassArr; [self presentViewController:sonRoot animated:NO completion:nil]; }}
查询效果:
若存在,则直接跳转界面: 在 FindSonViewController.h 中:#importNS_ASSUME_NONNULL_BEGIN@interface FindSonViewController : UIViewController @property UILabel *label1;@property UITableView *tableView;@property NSMutableArray *nameArr;@property NSMutableArray *ageArr;@property NSMutableArray *classArr;@end
在 FindSonViewController.m 中:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *backImage = [UIImage imageNamed:@"背景17.jpg"]; UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage]; backImageView.frame = self.view.bounds; [self.view insertSubview:backImageView atIndex:0]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 200, 300, 50)]; label1.textColor = [UIColor whiteColor]; label1.tintColor = [UIColor clearColor]; label1.font = [UIFont systemFontOfSize:28]; label1.text = @"您查找的学生信息为:"; [self.view addSubview:label1]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 290, [UIScreen mainScreen].bounds.size.width, 550) style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] init]; [_tableView registerClass:[FindSonTableViewCell class] forCellReuseIdentifier:@"111"]; UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(180, 570, 200, 50); [self.view addSubview:back]; [back setTitle:@"back" forState:UIControlStateNormal]; back.titleLabel.font = [UIFont systemFontOfSize:25]; back.tintColor = [UIColor whiteColor]; back.backgroundColor = [UIColor clearColor]; [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchDown]; }
使用tableView显示,在这里我强迫症,新建了一个cell,其实cell是和 FirstViewController 里 ableview 的cell一样,可用同一个,在这里就不再写了
找到后的界面: 如果有重复班级之内的: 这样,一个简单的学生管理系统就完啦转载地址:http://cukti.baihongyu.com/