IOS登陆+注册+抽奖+排行榜

时间:2023-11-02 08:35:02

要求:三个页面(登录页面,pickerView页面,排行榜页面),pickerView页面是三个组件,每个组件显示0-9,点击按钮进行随机,获得的值存入排行榜,排行榜显示大于500的最高的10个分数和对应的用户名,切换页面可以用任何方法(0-9循环显示,登录注销[可以有不同的用户],判断用户名是否为邮箱[正则表达式])

说明:
1.要修改xml文件到当前系统桌面,dic.xml保存的是用户名和密码,array.xml是保存的积分榜
2.正则表达式用在注册页面,在注册用户的时候用户名需要经过正则表达式的验证,判断是否是邮箱
3.有一个默认的admin,admin账户
4.排行榜显示是用的tableView显示的

疑惑:
1.如何创建文件在当前项目下
2.为什么view之间传数组传不了,我知道数组要先初始化
3.数组排序,用内置的排序方法,compare:,从大到小用什么参数
4.如何让tableView中的table随array的改变而刷新

项目源码:http://download.csdn.net/detail/s10141303/5970243

步骤:

注册view

IOS登陆+注册+抽奖+排行榜IOS登陆+注册+抽奖+排行榜IOS登陆+注册+抽奖+排行榜IOS登陆+注册+抽奖+排行榜

RegistViewController.h:

  1. #import <UIKit/UIKit.h>
  2. @class LoginViewController;
  3. @interface RegistViewController : UIViewController
  4. - (IBAction)click:(id)sender;
  5. @property (retain, nonatomic) IBOutlet UITextField *txtName;
  6. @property (retain, nonatomic) IBOutlet UITextField *txtpassword;
  7. @property (retain, nonatomic) IBOutlet UITextField *txtConfirmPassword;
  8. @property(retain,nonatomic) NSMutableDictionary *dic;
  9. @property(retain,nonatomic) LoginViewController *loginView;
  10. @end

RegistViewController.m:

  1. #import "RegistViewController.h"
  2. #import "info.h"
  3. #import "LoginViewController.h"
  4. #define OK_BUTTON 0  //登陆按钮
  5. #define CANCEL_BUTTON 1 //取消按钮
  6. #define BOARD 2  //键盘点击
  7. #define RETURN_BUTTON 3 //返回登陆界面
  8. @interface RegistViewController ()
  9. @end
  10. @implementation RegistViewController
  11. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  12. {
  13. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  14. if (self) {
  15. }
  16. return self;
  17. }
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. self.dic = [[[NSMutableDictionary alloc] init] autorelease];
  22. [self.dic setObject:@"admin" forKey:@"admin"];
  23. }
  24. - (IBAction)click:(id)sender {
  25. UIButton *button = (UIButton*)sender;
  26. //登陆按钮
  27. if (button.tag == OK_BUTTON)
  28. {
  29. //[[info getInfo] initDic];//初始化dic,包含了admin账号
  30. NSString *path = @"Users/zl201/Desktop/dic.xml";
  31. NSMutableDictionary *dicc = [NSMutableDictionary dictionaryWithContentsOfFile:path];
  32. NSLog(@"文件读取成功");
  33. NSLog(@"%@",dicc);
  34. //如果为空
  35. if([self.txtName.text isEqualToString:@""]||[self.txtpassword.text isEqualToString:@""]||[self.txtConfirmPassword.text isEqualToString:@""])
  36. {
  37. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"输入不能为空" delegate:nil cancelButtonTitle:@"重试" otherButtonTitles:nil];
  38. [a show];
  39. }
  40. else
  41. {
  42. //检测注册是是否是邮箱
  43. if ([[info getInfo] isValidateEmail:self.txtName.text]) {
  44. [self.txtName resignFirstResponder];
  45. [self.txtpassword resignFirstResponder];
  46. [self.txtConfirmPassword resignFirstResponder];
  47. //看是否包含某个键
  48. if ([[dicc allKeys] containsObject:self.txtName.text]) {
  49. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"该用户已经存在,请重新注册新用户名!" delegate:nil cancelButtonTitle:@"重试" otherButtonTitles:nil];
  50. [a show];
  51. self.txtName.text = @"";
  52. self.txtpassword.text = @"";
  53. self.txtConfirmPassword.text = @"";
  54. }
  55. else
  56. {
  57. //查看是否两次密码不一样
  58. if (![self.txtConfirmPassword.text isEqualToString:self.txtpassword.text]) {
  59. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"两次输入密码不一样" delegate:nil cancelButtonTitle:@"重试" otherButtonTitles:nil];
  60. [a show];
  61. self.txtConfirmPassword.text = @"";
  62. }
  63. //注册成功
  64. else{
  65. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"" message:@"恭喜注册成功,请点击返回登陆界面按钮!" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
  66. [a show];
  67. [info getInfo].name = self.txtName.text;
  68. [info getInfo].password = self.txtpassword.text;
  69. NSLog(@"%@,%@",[info getInfo].name,[info getInfo].password);
  70. //写入文件
  71. [self.dic setObject:self.txtpassword.text forKey:self.txtName.text];
  72. NSLog(@"%@",self.dic);
  73. NSString *path = @"Users/zl201/Desktop/dic.xml";
  74. [self.dic writeToFile:path atomically:YES];
  75. NSLog(@"文件写入成功");
  76. self.txtConfirmPassword.text = @"";
  77. self.txtpassword.text = @"";
  78. self.txtName.text = @"";
  79. }
  80. }
  81. }
  82. else  //不是邮箱
  83. {
  84. UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"用户名必须是邮箱" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  85. [aa show];
  86. }
  87. }
  88. }
  89. //取消按钮
  90. else if (button.tag == CANCEL_BUTTON)
  91. {
  92. self.txtName.text = @"";
  93. self.txtpassword.text = @"";
  94. self.txtConfirmPassword.text = @"";
  95. [self.txtName resignFirstResponder];
  96. [self.txtpassword resignFirstResponder];
  97. [self.txtConfirmPassword resignFirstResponder];
  98. }
  99. //面板触发退出键盘
  100. else if(button.tag == BOARD)
  101. {
  102. [self.txtName resignFirstResponder];
  103. [self.txtpassword resignFirstResponder];
  104. [self.txtConfirmPassword resignFirstResponder];
  105. }
  106. //返回登陆界面
  107. else if(button.tag == RETURN_BUTTON)
  108. {
  109. [self.loginView.view removeFromSuperview];
  110. self.loginView = [[[LoginViewController alloc]initWithNibName:@"LoginViewController"bundle:nil]autorelease];
  111. [self.view insertSubview:self.loginView.view atIndex:10];
  112. }
  113. }
  114. - (void)dealloc {
  115. [_txtName release];
  116. [_txtpassword release];
  117. [_txtConfirmPassword release];
  118. [_dic release];
  119. [super dealloc];
  120. }
  121. @end

登陆View

IOS登陆+注册+抽奖+排行榜IOS登陆+注册+抽奖+排行榜

LoginViewController.h:

  1. #import <UIKit/UIKit.h>
  2. @class RegistViewController;
  3. @class SecondViewController;
  4. @interface LoginViewController : UIViewController
  5. @property (retain, nonatomic) IBOutlet UITextField *txtName;
  6. @property (retain, nonatomic) IBOutlet UITextField *txtPassword;
  7. @property (retain,nonatomic)RegistViewController *registView;
  8. @property(retain,nonatomic)SecondViewController *secondView;
  9. - (IBAction)click:(id)sender;
  10. //接受保存当前登陆的账户和密码
  11. @property(retain,nonatomic) NSString *name;
  12. @property(retain,nonatomic) NSString *password;
  13. @end

LoginViewController.m:

  1. #import "LoginViewController.h"
  2. #import "info.h"
  3. #import "RegistViewController.h"
  4. #import "SecondViewController.h"
  5. #define OK_BUTTON 0  //登陆按钮
  6. #define CANCEL_BUTTON 1 //退出系统
  7. #define BOARD 2  //键盘点击
  8. #define RESIGN_BUTTON 3 //注销按钮
  9. #define REGISTER_BUTTON 4 //注册按钮
  10. @interface LoginViewController ()
  11. @end
  12. @implementation LoginViewController
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  14. {
  15. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  16. if (self) {
  17. // Custom initialization
  18. }
  19. return self;
  20. }
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. }
  25. -(void)viewDidAppear:(BOOL)animated
  26. {
  27. NSLog(@"%@",[info getInfo].name);
  28. NSLog(@"%@",[info getInfo].password);
  29. if ([info getInfo].name != NULL&&[info getInfo].password != NULL) {
  30. self.txtName.text = [info getInfo].name;
  31. self.txtPassword.text = [info getInfo].password;
  32. }
  33. }
  34. - (void)dealloc {
  35. [_txtName release];
  36. [_txtPassword release];
  37. [_registView release];
  38. [_secondView release];
  39. [_name release];
  40. [_password release];
  41. [super dealloc];
  42. }
  43. - (IBAction)click:(id)sender {
  44. UIButton *button = (UIButton*)sender;
  45. //登陆按钮
  46. if (button.tag == OK_BUTTON)
  47. {
  48. [self.txtName resignFirstResponder];
  49. [self.txtPassword resignFirstResponder];
  50. //读取xml文件
  51. NSString *path = @"Users/zl201/Desktop/dic.xml";
  52. NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
  53. NSLog(@"文件读取成功");
  54. NSLog(@"%@",dic);
  55. NSString *s =[dic objectForKey:self.txtName.text];
  56. if ([s isEqualToString:self.txtPassword.text]) {
  57. [info getInfo].name = self.txtName.text;
  58. [info getInfo].password=self.txtPassword.text;
  59. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"登陆状态" message:@"恭喜登陆成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
  60. [a show];
  61. }
  62. else
  63. {
  64. UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"登陆状态" message:@"登陆失败" delegate:nil cancelButtonTitle:@"重试" otherButtonTitles:nil];
  65. [a show];
  66. self.txtName.text = @"";
  67. self.txtPassword.text = @"";
  68. }
  69. }
  70. //系统退出
  71. else if (button.tag == CANCEL_BUTTON)
  72. {
  73. exit(0);
  74. }
  75. //面板触发退出键盘
  76. else if(button.tag == BOARD)
  77. {
  78. [self.txtName resignFirstResponder];
  79. [self.txtPassword resignFirstResponder];
  80. }
  81. //注销按钮
  82. else if(button.tag == RESIGN_BUTTON)
  83. {
  84. [info getInfo].name=@"";
  85. [info getInfo].password=@"";
  86. self.txtName.text = @"";
  87. self.txtPassword.text = @"";
  88. [self.txtName resignFirstResponder];
  89. [self.txtPassword resignFirstResponder];
  90. }
  91. //注册按钮
  92. else if(button.tag == REGISTER_BUTTON)
  93. {
  94. [self.registView.view removeFromSuperview];
  95. self.registView = [[[RegistViewController alloc]initWithNibName:@"RegistViewController" bundle:nil]autorelease];
  96. [self.view insertSubview:self.registView.view atIndex:10];
  97. }
  98. }
  99. @end

抽奖view

IOS登陆+注册+抽奖+排行榜IOS登陆+注册+抽奖+排行榜

ViewController.h:

  1. #import <UIKit/UIKit.h>
  2. @class ShowViewController;
  3. @interface SecondViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
  4. @property (retain, nonatomic) IBOutlet UIPickerView *picker;
  5. - (IBAction)click:(id)sender;
  6. - (IBAction)OK:(id)sender;
  7. @property(nonatomic,retain)NSArray *images;
  8. @property(nonatomic,retain)NSArray *column1;
  9. @property(nonatomic,retain)NSArray *column2;
  10. @property(nonatomic,retain)NSArray *column3;
  11. @property(nonatomic,retain)NSArray *column4;
  12. @property(nonatomic,retain)NSArray *column5;
  13. @property(nonatomic,assign)long num;
  14. @property (retain, nonatomic) IBOutlet UILabel *lblShow;
  15. @property(nonatomic,retain)ShowViewController *showView;
  16. @property(nonatomic,retain)NSMutableArray *array;//记录分数
  17. @end

ViewController.m:

  1. #import "SecondViewController.h"
  2. #import <AudioToolbox/AudioToolbox.h>
  3. #import "info.h"
  4. @interface SecondViewController ()
  5. @end
  6. @implementation SecondViewController
  7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  8. {
  9. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  10. if (self) {
  11. // Custom initialization
  12. }
  13. return self;
  14. }
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. self.array = [[NSMutableArray alloc] init];
  19. UIImage *image1 = [UIImage imageNamed:@"apple.png"];
  20. UIImage *image2 = [UIImage imageNamed:@"bar.png"];
  21. UIImage *image3 = [UIImage imageNamed:@"cherry.png"];
  22. UIImage *image4 = [UIImage imageNamed:@"crown.png"];
  23. UIImage *image5 = [UIImage imageNamed:@"lemon.png"];
  24. UIImage *image6 = [UIImage imageNamed:@"seven.png"];
  25. self.images = @[image1,image2,image3,image4,image5,image6];
  26. //创建30个ImageView
  27. for (int i=0; i<5; i++) {
  28. UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
  29. UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
  30. UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image3];
  31. UIImageView *imageView4 = [[UIImageView alloc] initWithImage:image4];
  32. UIImageView *imageView5 = [[UIImageView alloc] initWithImage:image5];
  33. UIImageView *imageView6 = [[UIImageView alloc] initWithImage:image6];
  34. NSArray *arr = @[imageView1,imageView2,imageView3,imageView4,imageView5,imageView6];
  35. NSString *str = [NSString stringWithFormat:@"column%d",i+1];
  36. //OC特有方法,对一个可能存在可能不存在的变量赋值,本来是一个变量,可以转化成字符串,这样就可以改变字符串了
  37. [self setValue:arr forKey:str]; //KVC
  38. [imageView1 release];
  39. [imageView2 release];
  40. [imageView3 release];
  41. [imageView4 release];
  42. [imageView5 release];
  43. [imageView6 release];
  44. }
  45. srandom(time(NULL));
  46. //默认选择在第5000行
  47. for (int i=0; i<5; i++) {
  48. [self.picker selectRow:5000 inComponent:i animated:NO];
  49. }
  50. }
  51. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  52. {
  53. return 5;
  54. }
  55. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  56. {
  57. return [self.images count]*10000;
  58. }
  59. -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  60. {
  61. //这种写法非常消耗内存
  62. //    UIImageView *imageView = [[UIImage alloc] initWithImage:[self.images objectAtIndex:row]];
  63. //    return imageView;
  64. NSString * str = [NSString stringWithFormat:@"column%d",component+1];
  65. NSArray *arr = [self valueForKey:str];
  66. return [arr objectAtIndex:row%6];
  67. }
  68. - (void)dealloc {
  69. [_picker release];
  70. [_images release];
  71. [_column1 release];
  72. [_column2 release];
  73. [_column3 release];
  74. [_column4 release];
  75. [_column5 release];
  76. [_showView release];
  77. [_lblShow release];
  78. [super dealloc];
  79. }
  80. //退出
  81. - (IBAction)click:(id)sender {
  82. //    NSLog(@"%@",srandom(time(nil)));
  83. exit(0);
  84. }
  85. //确定
  86. - (IBAction)OK:(id)sender {
  87. int a[5];
  88. bool f=false;
  89. for (int i=0; i<5; i++) {
  90. int row = random() % [self.column1 count];
  91. int n = random() % 35;
  92. int j = row *n;
  93. [self.picker selectRow:j inComponent:i animated:YES];
  94. a[i]=j%6;
  95. }
  96. int sum=0;
  97. for (int i=0; i<5; i++)
  98. {
  99. sum = 1;
  100. if (f==false)
  101. {
  102. for (int j=i+1; j<5; j++)
  103. {
  104. if (a[i] == a[j])
  105. {
  106. sum++;
  107. }
  108. if (sum>=3)
  109. {
  110. f=true;
  111. break;
  112. }
  113. }
  114. }
  115. else
  116. {
  117. break;
  118. }
  119. }
  120. if (f) {
  121. //        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"我中了" message:@"中了500万" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
  122. //        [a show];
  123. //[self playMusic:@"win"];//调用播放音乐
  124. }
  125. [self playMusic:@"crunch"];
  126. self.num = a[0]*10000+a[1]*1000+a[2]*100+a[3]*10+a[4];
  127. [info getInfo].number = self.num;
  128. NSLog(@"%d",self.num);
  129. NSLog(@"%@",[info getInfo].name);
  130. //self.lblScore.text = [NSString stringWithFormat:"当前得分:%d",self.num];
  131. self.lblShow.text = @"";
  132. NSLog(@"%@",[info getInfo].name);
  133. if ([info getInfo].name!=NULL&&self.num>=4000) {
  134. NSString *str = [NSString stringWithFormat:@"恭喜%@获得%d的高分上榜了",[info getInfo].name,self.num];
  135. self.lblShow.text = str;
  136. CGSize size = [self.lblShow.text sizeWithFont:self.lblShow.font];
  137. CGRect frame = CGRectMake(self.lblShow.frame.origin.x, self.lblShow.frame.origin.y, size.width, self.lblShow.frame.size.height);
  138. self.lblShow.frame = frame;
  139. NSLog(@"恭喜获得4000以上的高分");
  140. [self.array addObject:[NSString stringWithFormat:@"%@                %d",[info getInfo].name,self.num]];
  141. NSLog(@"%@",self.array);
  142. NSString *path = @"Users/zl201/Desktop/array.xml";
  143. [self.array writeToFile:path atomically:YES];
  144. NSLog(@"文件写入成功");
  145. }
  146. else if ([info getInfo].name!=NULL&&self.num<4000)
  147. {
  148. NSString *str = [NSString stringWithFormat:@"%@,很遗憾只有%d分,不足4000,加油",[info getInfo].name,self.num];
  149. self.lblShow.text = str;
  150. CGSize size = [self.lblShow.text sizeWithFont:self.lblShow.font];
  151. CGRect frame = CGRectMake(self.lblShow.frame.origin.x, self.lblShow.frame.origin.y, size.width, self.lblShow.frame.size.height);
  152. self.lblShow.frame = frame;
  153. }
  154. }
  155. -(void)playMusic:(NSString*)s
  156. {
  157. //通过NSBundle来获得音频文件的url地址
  158. NSURL *url = [[NSBundle mainBundle] URLForResource:s withExtension:@"wav"];
  159. SystemSoundID winSound;//整形类型的id
  160. //用音频服务,为音频文件绑定soundID
  161. AudioServicesCreateSystemSoundID((CFURLRef)url, &winSound);
  162. //通过绑定的soundId来播放音乐
  163. AudioServicesPlayAlertSound(winSound);
  164. }@end

排行榜View

IOS登陆+注册+抽奖+排行榜

ShowViewController.h:

  1. #import <UIKit/UIKit.h>
  2. @class SecondViewController;
  3. @interface ShowViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
  4. @property (retain, nonatomic) IBOutlet UITableView *tableView;
  5. @property(nonatomic,retain)NSMutableArray *array;//加上头行显示给tableView
  6. @property(nonatomic,retain)NSMutableArray *array1;//记录分数
  7. @property(nonatomic,retain)SecondViewController *secView;
  8. @end

ShowViewController.m:

    1. #import "ShowViewController.h"
    2. #import "SecondViewController.h"
    3. @interface ShowViewController ()
    4. @end
    5. @implementation ShowViewController
    6. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    7. {
    8. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    9. if (self) {
    10. // Custom initialization
    11. }
    12. return self;
    13. }
    14. - (void)viewDidLoad
    15. {
    16. [super viewDidLoad];
    17. NSString *path = @"Users/zl201/Desktop/array.xml";
    18. self.array1 = [NSArray arrayWithContentsOfFile:path];
    19. NSLog(@"文件读入成功");
    20. NSLog(@"%@",self.array1);
    21. [self.array1 sortUsingSelector:@selector(compare:options:)];   //如何倒序
    22. NSLog(@"%@",self.array1);
    23. self.array = [NSMutableArray arrayWithObject:@"积分排行榜(>=4000):"];
    24. [self.array addObjectsFromArray:self.array1];
    25. }
    26. - (void)didReceiveMemoryWarning
    27. {
    28. [super didReceiveMemoryWarning];
    29. // Dispose of any resources that can be recreated.
    30. }
    31. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    32. {
    33. return 1;
    34. }
    35. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    36. {
    37. return [self.array count];
    38. }
    39. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    40. {
    41. NSString *MyIdentifier = @"MyIdentifier";
    42. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    43. if (cell == nil) {
    44. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    45. }
    46. NSString  *titleStr = [self.array objectAtIndex:indexPath.row];
    47. cell.textLabel.text = titleStr;
    48. return cell;
    49. }
    50. -(void)dealloc
    51. {
    52. [_array release];
    53. [_tableView release];
    54. [super dealloc];
    55. }
    56. @end