我的学习日志009:“密码生成器”

交代一下我做程序的工具:mac os x虚拟机10.9.3         Xcode6         百度^-^         参考书iPhone30天精通


//  ViewController.h
//  9
//
//  Created by 李迪 on 15-7-26.
//  Copyright (c) 2015年 李迪. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface ViewController : UIViewController<UITextFieldDelegate>{
    IBOutlet UITextField * passWord;
    IBOutlet UITextField * passWordLength;
    //两个文本框,分别用来显示密码和添加密码长度
    IBOutlet UISwitch * includeUpperCase;
    IBOutlet UISwitch * includeLowerCase;
    IBOutlet UISwitch * includeNumbers;
    //三个switch开关
}
@property UITextField * passWord;
@property UITextField * passWordLength;
@property UISwitch * includeUpperCase;
@property UISwitch * includeLowerCase;
@property UISwitch * includeNumbers;

-(IBAction)setPassWord;//生成密码按钮,按它生成密码

@end





//  ViewController.m
//  9
//
//  Created by 李迪 on 15-7-26.
//  Copyright (c) 2015年 李迪. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
            

@end

@implementation ViewController

@synthesize passWord,passWordLength,includeUpperCase,includeLowerCase,includeNumbers;

#define RANDOM_SEED() strandom(time(NULL))
#define RANDOM_INT(__MIN__,__MAX__) ((__MIN__)+random()%((__MAX__+1)-(__MIN__)))
//两个代理
//第一个代理是产生随机种子,使随机数产生时是真实的随机。
//第二个代理是生成一个随机数,这个随机数的取值范围是__MAX__和__MIN__之间的一个数,在本程序中的取值范围是在0~passWordChars-1;
//        也可以不使用代理,用rand()%(passWordChars-1)来实现。


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    if(touch.tapCount>=1){
        [passWord resignFirstResponder];
        [passWordLength resignFirstResponder];
    }
}//这个方法是触摸屏幕的时候键盘也消失,之前第七个程序的时候学到的。

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == passWord)
        [passWord resignFirstResponder];//按键盘的return按键收起键盘
    if(textField == passWordLength)
        [passWordLength resignFirstResponder];//按键盘的return按键收起键盘
    return YES;
}//本方法是代理方法,要实现textFieldDelegate协议才能生效,实现协议的同时还要设置代理,即passWord.delegate = self passWordLength.delegate = self;(本程序是在viewDidLoad方法里面实现的)

-(IBAction)setPassWord{
    NSInteger iPassWordLength = [passWordLength.text intValue];
    //得到密码长度文本框里面的数字
    BOOL bIncludeLowerCase = includeLowerCase.on;
    BOOL bIncludeUpperCase = includeUpperCase.on;
    BOOL bIncludeNumbers = includeNumbers.on;
    //这三个布尔对象分别代表switch开关开启时的状态
    NSString *passWordText = @"";
    //passWoedText代表的是显示密码文本框里面的内容,开始时是空的
    NSString *lowerCaseChars = @"abcdefghijklmnopqrstuvwxyz";
    NSString *upperCaseChars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSString *numbers = @"0123456789";
    //三个字符串,分别是大写字母,小写字母和数字
    NSString *passWordChars = @"";
    //passWordChars表示需要满足的条件满足时,把所有可以使用的字符放到一起时的字符串
    if(bIncludeLowerCase)
        passWordChars = [NSString stringWithFormat:@"%@%@",passWordChars,lowerCaseChars];
    if(bIncludeUpperCase)
        passWordChars = [NSString stringWithFormat:@"%@%@",passWordChars,upperCaseChars];
    if(bIncludeNumbers)
        passWordChars = [NSString stringWithFormat:@"%@%@",passWordChars,numbers];
    //几个if语句就是满足需要满足的情况,然后把所有可能用到的字符串整理到passWordChars字符串上
    for(NSInteger i =0;i<iPassWordLength;i++){
        int index = RANDOM_INT(0,[passWordChars length]-1);//这里用到的就是前面定义好的宏,取一个在passWordChars范围内的下标
        NSRange range = NSMakeRange(index, 1);//定义一个范围,表示从下标index开始,1个字符
        NSString *passWordChar = [passWordChars substringWithRange:range];//根据字符串得到一个子字符串
        passWordText = [NSString stringWithFormat:@"%@%@",passWordText,passWordChar];//将子字符串添加到passWordText后面
    }
    passWord.text = passWordText;//在文本框上显示出来
}
            
- (void)viewDidLoad {
    passWord.delegate = self;
    passWordLength.delegate = self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值