我的学习日志009:“密码生成器”
2015-07-27 16:19
267 查看
交代一下我做程序的工具: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
// 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
相关文章推荐
- 如何使用iOS 8的虚化效果(Swift版本)
- Nagios邮件报警设置(解决邮件中文乱码问题)
- Rails Mail发送不成功
- JS 错误处理与调试
- 【工作笔记0005】IIS6.0 伪静态设置,伪静态规则
- NYOJ 541 最强DE 战斗力(大数水)
- 参数(parameter)和属性(Attribute)的区别
- screen & tmux 技巧:linux后台运行程序
- 大型数据库应用解决方案总结
- iOS的面向对象(类和对象)
- ZeroMQ——一个轻量级的消息通信组件 C#
- Nexus私服搭建
- 机房合作--简单封装
- mysql IFNULL IF
- SQL Server遇到的错误和有用的tools
- searchDisplayController 引起的数组越界处理办法
- Java中ArrayList类的用法(转)
- Android 保存数据到文件
- EALayout 第六节 LinerLayout线性布局器
- 数位DP模板