IOS登錄+註冊+抽獎+排行榜

要求:三個頁面(登陸頁面,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

 

 

#import "RegistViewController.h"
#import "info.h"
#import "LoginViewController.h"
#define OK_BUTTON 0  //登錄按鈕
#define CANCEL_BUTTON 1 //取消按鈕
#define BOARD 2  //鍵盤點擊
#define RETURN_BUTTON 3 //返回登錄界面
@interface RegistViewController ()

@end

@implementation RegistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dic = [[[NSMutableDictionary alloc] init] autorelease];
    [self.dic setObject:@"admin" forKey:@"admin"];
}

- (IBAction)click:(id)sender {
    UIButton *button = (UIButton*)sender;
    //登錄按鈕
    if (button.tag == OK_BUTTON)
    {
        //[[info getInfo] initDic];//初始化dic,包含了admin帳號
        NSString *path = @"Users/zl201/Desktop/dic.xml";
        NSMutableDictionary *dicc = [NSMutableDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"文件讀取成功");
        NSLog(@"%@",dicc);
        //若是爲空
        if([self.txtName.text isEqualToString:@""]||[self.txtpassword.text isEqualToString:@""]||[self.txtConfirmPassword.text isEqualToString:@""])
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"輸入不能爲空" delegate:nil cancelButtonTitle:@"重試" otherButtonTitles:nil];
            [a show];
        }
        else
        {
            //檢測註冊是是不是郵箱
            if ([[info getInfo] isValidateEmail:self.txtName.text]) {
                
            [self.txtName resignFirstResponder];
            [self.txtpassword resignFirstResponder];
            [self.txtConfirmPassword resignFirstResponder];
            //看是否包含某個鍵
            if ([[dicc allKeys] containsObject:self.txtName.text]) {
                UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"該用戶已經存在,請從新註冊新用戶名!" delegate:nil cancelButtonTitle:@"重試" otherButtonTitles:nil];
                [a show];
                self.txtName.text = @"";
                self.txtpassword.text = @"";
                self.txtConfirmPassword.text = @"";
            }
            else
            {
                //查看是否兩次密碼不同
                if (![self.txtConfirmPassword.text isEqualToString:self.txtpassword.text]) {
                UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"兩次輸入密碼不同" delegate:nil cancelButtonTitle:@"重試" otherButtonTitles:nil];
                    [a show];
                    self.txtConfirmPassword.text = @"";
                }
                //註冊成功
                else{
                    UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"" message:@"恭喜註冊成功,請點擊返回登錄界面按鈕!" delegate:nil cancelButtonTitle:@"確認" otherButtonTitles:nil];
                    [a show];
                    
                    [info getInfo].name = self.txtName.text;
                    [info getInfo].password = self.txtpassword.text;
                    NSLog(@"%@,%@",[info getInfo].name,[info getInfo].password);

                    //寫入文件
                    [self.dic setObject:self.txtpassword.text forKey:self.txtName.text];
                    NSLog(@"%@",self.dic);
                    NSString *path = @"Users/zl201/Desktop/dic.xml";
                    [self.dic writeToFile:path atomically:YES];
                    NSLog(@"文件寫入成功");
                    
                    self.txtConfirmPassword.text = @"";
                    self.txtpassword.text = @"";
                    self.txtName.text = @"";
                }
            }
            }
            else  //不是郵箱
            {
                UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"用戶名必須是郵箱" delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
                [aa show];
            }
        }
    }
    //取消按鈕
    else if (button.tag == CANCEL_BUTTON)
    {
        self.txtName.text = @"";
        self.txtpassword.text = @"";
        self.txtConfirmPassword.text = @"";
        [self.txtName resignFirstResponder];
        [self.txtpassword resignFirstResponder];
        [self.txtConfirmPassword resignFirstResponder];
    }
    //面板觸發退出鍵盤
    else if(button.tag == BOARD)
    {
        [self.txtName resignFirstResponder];
        [self.txtpassword resignFirstResponder];
        [self.txtConfirmPassword resignFirstResponder];
    }
    //返回登錄界面
    else if(button.tag == RETURN_BUTTON)
    {
        [self.loginView.view removeFromSuperview];
        
        self.loginView = [[[LoginViewController alloc]initWithNibName:@"LoginViewController"bundle:nil]autorelease];
        [self.view insertSubview:self.loginView.view atIndex:10];
    }
}
- (void)dealloc {
    [_txtName release];
    [_txtpassword release];
    [_txtConfirmPassword release];
    [_dic release];
    [super dealloc];
}
@end


登錄View.net

 


LoginViewController.h:code

 

#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:component

 

 

#import "LoginViewController.h"
#import "info.h"
#import "RegistViewController.h"
#import "SecondViewController.h"
#define OK_BUTTON 0  //登錄按鈕
#define CANCEL_BUTTON 1 //退出系統
#define BOARD 2  //鍵盤點擊
#define RESIGN_BUTTON 3 //註銷按鈕
#define REGISTER_BUTTON 4 //註冊按鈕
@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%@",[info getInfo].name);
    NSLog(@"%@",[info getInfo].password);
        if ([info getInfo].name != NULL&&[info getInfo].password != NULL) {
            self.txtName.text = [info getInfo].name;
            self.txtPassword.text = [info getInfo].password;
        }
}

- (void)dealloc {
    [_txtName release];
    [_txtPassword release];
    [_registView release];
    [_secondView release];
    [_name release];
    [_password release];
    [super dealloc];
}
- (IBAction)click:(id)sender {
    UIButton *button = (UIButton*)sender;
    //登錄按鈕
    if (button.tag == OK_BUTTON)
    {
        [self.txtName resignFirstResponder];
        [self.txtPassword resignFirstResponder];
        //讀取xml文件
        NSString *path = @"Users/zl201/Desktop/dic.xml";
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"文件讀取成功");
        NSLog(@"%@",dic);
        NSString *s =[dic objectForKey:self.txtName.text];

        if ([s isEqualToString:self.txtPassword.text]) {
            [info getInfo].name = self.txtName.text;
            [info getInfo].password=self.txtPassword.text;
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"登錄狀態" message:@"恭喜登錄成功" delegate:nil cancelButtonTitle:@"確認" otherButtonTitles:nil];
            [a show];
            
        }
        else
        {
            UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"登錄狀態" message:@"登錄失敗" delegate:nil cancelButtonTitle:@"重試" otherButtonTitles:nil];
            [a show];
            self.txtName.text = @"";
            self.txtPassword.text = @"";
        }
    }
    //系統退出
    else if (button.tag == CANCEL_BUTTON)
    {
        exit(0);
    }
    //面板觸發退出鍵盤
    else if(button.tag == BOARD)
    {
        [self.txtName resignFirstResponder];
        [self.txtPassword resignFirstResponder];
    }
    //註銷按鈕
    else if(button.tag == RESIGN_BUTTON)
    {
        [info getInfo].name=@"";
        [info getInfo].password=@"";
        self.txtName.text = @"";
        self.txtPassword.text = @"";
        [self.txtName resignFirstResponder];
        [self.txtPassword resignFirstResponder];
    }
    //註冊按鈕
    else if(button.tag == REGISTER_BUTTON)
    {
        
        [self.registView.view removeFromSuperview];
        
        self.registView = [[[RegistViewController alloc]initWithNibName:@"RegistViewController" bundle:nil]autorelease];
        [self.view insertSubview:self.registView.view atIndex:10];
        
    }
}
@end


抽獎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
相關文章
相關標籤/搜索