要求:三个页面(登陆页面,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/5970243app
步骤:dom
注册viewatom




RegistViewController.h:url
- #import <UIKit/UIKit.h>
- @class LoginViewController;
- @interface RegistViewController : UIViewController
- - (IBAction)click:(id)sender;
- @property (retain, nonatomic) IBOutlet UITextField *txtName;
- @property (retain, nonatomic) IBOutlet UITextField *txtpassword;
- @property (retain, nonatomic) IBOutlet UITextField *txtConfirmPassword;
- @property(retain,nonatomic) NSMutableDictionary *dic;
- @property(retain,nonatomic) LoginViewController *loginView;
- @end
RegistViewController.m:spa
登录View.net


LoginViewController.h:component
- #import <UIKit/UIKit.h>
- @class RegistViewController;
- @class SecondViewController;
- @interface LoginViewController : UIViewController
- @property (retain, nonatomic) IBOutlet UITextField *txtName;
- @property (retain, nonatomic) IBOutlet UITextField *txtPassword;
- @property (retain,nonatomic)RegistViewController *registView;
- @property(retain,nonatomic)SecondViewController *secondView;
- - (IBAction)click:(id)sender;
- //接受保存当前登录的帐户和密码
- @property(retain,nonatomic) NSString *name;
- @property(retain,nonatomic) NSString *password;
- @end
LoginViewController.m:orm
抽奖view


ViewController.h:
- #import <UIKit/UIKit.h>
- @class ShowViewController;
- @interface SecondViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
- @property (retain, nonatomic) IBOutlet UIPickerView *picker;
- - (IBAction)click:(id)sender;
- - (IBAction)OK:(id)sender;
- @property(nonatomic,retain)NSArray *images;
- @property(nonatomic,retain)NSArray *column1;
- @property(nonatomic,retain)NSArray *column2;
- @property(nonatomic,retain)NSArray *column3;
- @property(nonatomic,retain)NSArray *column4;
- @property(nonatomic,retain)NSArray *column5;
- @property(nonatomic,assign)long num;
- @property (retain, nonatomic) IBOutlet UILabel *lblShow;
- @property(nonatomic,retain)ShowViewController *showView;
- @property(nonatomic,retain)NSMutableArray *array;//记录分数
- @end
ViewController.m:
- #import "SecondViewController.h"
- #import <AudioToolbox/AudioToolbox.h>
- #import "info.h"
- @interface SecondViewController ()
-
- @end
-
- @implementation SecondViewController
-
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.array = [[NSMutableArray alloc] init];
- UIImage *image1 = [UIImage imageNamed:@"apple.png"];
- UIImage *image2 = [UIImage imageNamed:@"bar.png"];
- UIImage *image3 = [UIImage imageNamed:@"cherry.png"];
- UIImage *image4 = [UIImage imageNamed:@"crown.png"];
- UIImage *image5 = [UIImage imageNamed:@"lemon.png"];
- UIImage *image6 = [UIImage imageNamed:@"seven.png"];
- self.images = @[image1,image2,image3,image4,image5,image6];
-
- //建立30个ImageView
- for (int i=0; i<5; i++) {
- UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
- UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
- UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image3];
- UIImageView *imageView4 = [[UIImageView alloc] initWithImage:image4];
- UIImageView *imageView5 = [[UIImageView alloc] initWithImage:image5];
- UIImageView *imageView6 = [[UIImageView alloc] initWithImage:image6];
- NSArray *arr = @[imageView1,imageView2,imageView3,imageView4,imageView5,imageView6];
- NSString *str = [NSString stringWithFormat:@"column%d",i+1];
- //OC特有方法,对一个可能存在可能不存在的变量赋值,原本是一个变量,能够转化成字符串,这样就能够改变字符串了
- [self setValue:arr forKey:str]; //KVC
- [imageView1 release];
- [imageView2 release];
- [imageView3 release];
- [imageView4 release];
- [imageView5 release];
- [imageView6 release];
- }
- srandom(time(NULL));
- //默认选择在第5000行
- for (int i=0; i<5; i++) {
- [self.picker selectRow:5000 inComponent:i animated:NO];
- }
-
- }
-
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 5;
- }
-
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return [self.images count]*10000;
- }
-
- -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- {
- //这种写法很是消耗内存
- // UIImageView *imageView = [[UIImage alloc] initWithImage:[self.images objectAtIndex:row]];
- // return imageView;
- NSString * str = [NSString stringWithFormat:@"column%d",component+1];
- NSArray *arr = [self valueForKey:str];
- return [arr objectAtIndex:row%6];
-
- }
-
- - (void)dealloc {
- [_picker release];
- [_images release];
- [_column1 release];
- [_column2 release];
- [_column3 release];
- [_column4 release];
- [_column5 release];
- [_showView release];
- [_lblShow release];
- [super dealloc];
- }
- //退出
- - (IBAction)click:(id)sender {
- // NSLog(@"%@",srandom(time(nil)));
- exit(0);
- }
- //肯定
- - (IBAction)OK:(id)sender {
- int a[5];
- bool f=false;
- for (int i=0; i<5; i++) {
- int row = random() % [self.column1 count];
- int n = random() % 35;
- int j = row *n;
- [self.picker selectRow:j inComponent:i animated:YES];
- a[i]=j%6;
- }
- int sum=0;
- for (int i=0; i<5; i++)
- {
- sum = 1;
- if (f==false)
- {
- for (int j=i+1; j<5; j++)
- {
- if (a[i] == a[j])
- {
- sum++;
- }
- if (sum>=3)
- {
- f=true;
- break;
- }
- }
- }
- else
- {
- break;
- }
- }
- if (f) {
- // UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"我中了" message:@"中了500万" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
- // [a show];
- //[self playMusic:@"win"];//调用播放音乐
- }
- [self playMusic:@"crunch"];
- self.num = a[0]*10000+a[1]*1000+a[2]*100+a[3]*10+a[4];
- [info getInfo].number = self.num;
- NSLog(@"%d",self.num);
- NSLog(@"%@",[info getInfo].name);
-
-
- //self.lblScore.text = [NSString stringWithFormat:"当前得分:%d",self.num];
- self.lblShow.text = @"";
- NSLog(@"%@",[info getInfo].name);
- if ([info getInfo].name!=NULL&&self.num>=4000) {
-
- NSString *str = [NSString stringWithFormat:@"恭喜%@得到%d的高分上榜了",[info getInfo].name,self.num];
- self.lblShow.text = str;
- CGSize size = [self.lblShow.text sizeWithFont:self.lblShow.font];
- CGRect frame = CGRectMake(self.lblShow.frame.origin.x, self.lblShow.frame.origin.y, size.width, self.lblShow.frame.size.height);
- self.lblShow.frame = frame;
- NSLog(@"恭喜得到4000以上的高分");
- [self.array addObject:[NSString stringWithFormat:@"%@ %d",[info getInfo].name,self.num]];
- NSLog(@"%@",self.array);
- NSString *path = @"Users/zl201/Desktop/array.xml";
- [self.array writeToFile:path atomically:YES];
- NSLog(@"文件写入成功");
- }
- else if ([info getInfo].name!=NULL&&self.num<4000)
- {
- NSString *str = [NSString stringWithFormat:@"%@,很遗憾只有%d分,不足4000,加油",[info getInfo].name,self.num];
- self.lblShow.text = str;
- CGSize size = [self.lblShow.text sizeWithFont:self.lblShow.font];
- CGRect frame = CGRectMake(self.lblShow.frame.origin.x, self.lblShow.frame.origin.y, size.width, self.lblShow.frame.size.height);
- self.lblShow.frame = frame;
- }
- }
-
- -(void)playMusic:(NSString*)s
- {
- //经过NSBundle来得到音频文件的url地址
- NSURL *url = [[NSBundle mainBundle] URLForResource:s withExtension:@"wav"];
- SystemSoundID winSound;//整形类型的id
- //用音频服务,为音频文件绑定soundID
- AudioServicesCreateSystemSoundID((CFURLRef)url, &winSound);
- //经过绑定的soundId来播放音乐
- AudioServicesPlayAlertSound(winSound);
- }@end
排行榜View

ShowViewController.h:
- #import <UIKit/UIKit.h>
- @class SecondViewController;
- @interface ShowViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
- @property (retain, nonatomic) IBOutlet UITableView *tableView;
- @property(nonatomic,retain)NSMutableArray *array;//加上头行显示给tableView
- @property(nonatomic,retain)NSMutableArray *array1;//记录分数
- @property(nonatomic,retain)SecondViewController *secView;
- @end
ShowViewController.m:
- #import "ShowViewController.h"
- #import "SecondViewController.h"
- @interface ShowViewController ()
-
- @end
-
- @implementation ShowViewController
-
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *path = @"Users/zl201/Desktop/array.xml";
- self.array1 = [NSArray arrayWithContentsOfFile:path];
- NSLog(@"文件读入成功");
- NSLog(@"%@",self.array1);
- [self.array1 sortUsingSelector:@selector(compare:options:)]; //如何倒序
- NSLog(@"%@",self.array1);
- self.array = [NSMutableArray arrayWithObject:@"积分排行榜(>=4000):"];
- [self.array addObjectsFromArray:self.array1];
-
- }
-
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.array count];
- }
-
- - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSString *MyIdentifier = @"MyIdentifier";
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
- }
- NSString *titleStr = [self.array objectAtIndex:indexPath.row];
-
- cell.textLabel.text = titleStr;
- return cell;
- }
-
- -(void)dealloc
- {
- [_array release];
- [_tableView release];
- [super dealloc];
- }
-
- @end