读书人

本次的学习的内容是关于在button动态方

发布时间: 2013-10-16 11:29:46 作者: rapoo

这次的学习的内容是关于在button动态方法连接多个switch 和textfield
目标:这次的学习的内容是关于在button动态方法连接多个switch 和textfield, switch判定textfield的显示结果, 使用代码获取button动态方法,并且在interfaceBuilder内对button,switch和textfield进行动态交互连接,然后显示运行结果。
编码:

//

// ViewController.h

// hhhh

//

// Created by bitcar on 12-7-10.

// Copyright (c) 2012年 bitcar . All rights reserved.

//


#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>


@interface ViewController : UIViewController


{

IBOutlet UITextField *passwordlength;

IBOutlet UITextField *password;

IBOutlet UISwitch *includeUpperCase;

IBOutlet UISwitch *includeLowerCase;

IBOutlet UISwitch *includeNumbers;

}


@property (nonatomic, retain) UITextField *passwordlength;


@property (nonatomic, retain) UITextField *password;


@property (nonatomic, retain) UISwitch *includeUpperCase;


@property (nonatomic, retain) UISwitch *includeLowerCase;


@property (nonatomic, retain) UISwitch *includeNumbers;



//-(IBAction)setPassword:(id)sender;


- (IBAction)onPasswordButtonClick:(id)sender;


@end



#import "ViewController.h"


@implementation ViewController


@synthesize passwordlength, password;


@synthesize includeLowerCase, includeNumbers, includeUpperCase;


#define RANDOM_SEED() srandom(time(NULL))


//定义随机值,用最小值和最大值和随机数来计算,返回整数值

#define RANDOM_INT(_MIN_, _MAX_) ((_MIN_) +random() % ((_MAX_ +1) - (_MIN_)))


//控件输入返回判定方法

- (BOOL) textFieldShouldReturn: (UITextField*)textField

{

if(textField == password)

{

[password resignFirstResponder];//隐藏密码文本框输入的键盘

}

if(textField == passwordlength)

{

[passwordlength resignFirstResponder];//隐藏密码长度文本框输入的键盘

}


return YES;

}


//button方法,点击的密码显示在文本框里

//-(IBAction)setPassword:(id)sender

- (IBAction)onPasswordButtonClick:(id)sender

{

//输入密码长度

NSInteger iPasswordLength = [passwordlength.text intValue];

//打开字母小写

BOOL bIncludeLowerCase = includeLowerCase.on;

//打开字母大写

BOOL bIncludeUpperCase = includeUpperCase.on;

//打开数字

BOOL bIncludeNumbers = includeNumbers.on;

NSString *passwordText = @"";

//定义a到z字母

NSString *lowercaseChars = @"abcdefghijklmnopqrstuvwxyz";

//定义A到Z字母

NSString *uppercaseChars = @"ABCDEFGHIGKLMNOPQRSTUVWXYZ";

//定义数字

NSString *numbersChars = @"1234567890";

//随机变量

RANDOM_SEED();

//字符对象为空

NSString *passwordChars = @"";

//字母小写的条件语句

if(bIncludeLowerCase)

{

passwordChars =

[NSString stringWithFormat:@"%@%@", passwordChars, lowercaseChars];

}

//字母大写的条件语句

if (bIncludeUpperCase) {

passwordChars =

[NSString stringWithFormat:@"%@%@", passwordChars, uppercaseChars];

}

//字母为数字的条件语句

if (bIncludeNumbers) {

passwordChars =

[NSString stringWithFormat:@"%@%@", passwordChars, numbersChars];

}

//数值从0开始,当数值小于密码长度,取得数字字符,获取的数据转换为文字格式

for (NSInteger i=0; i<iPasswordLength; i++)

{

int index = RANDOM_INT(0, [passwordChars length]-1);


NSRange range = NSMakeRange(index,1);

NSString *passwordChar = [passwordChars substringWithRange:range];

passwordText =

[NSString stringWithFormat:@"%@%@", passwordText, passwordChar];

}


password.text = @"";


password.text = passwordText;


}


- (void)viewDidLoad

{

[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.

}


- (void) dealloc

{

[passwordlength release];

[password release];

[includeNumbers release];

[includeLowerCase release];

[includeUpperCase release];

[super dealloc];

}


@end




运行程序:本次的学习的内容是关于在button动态方法连接多个switch 和textfield
当把所有字母大写小写和数字开启时。
本次的学习的内容是关于在button动态方法连接多个switch 和textfield
当只把字母大写关闭时。
本次的学习的内容是关于在button动态方法连接多个switch 和textfield
当把数字开启时。
总结:@property (nonatomic, retain) UITextField *password;

//-(IBAction)setPassword:(id)sender;


这次的编码因为系统误认set,get的原因让程序不能执行,我将记录下并分析其中的原因。


当把“按扭生成密码“这个方法写成setPassword时,系统出现如下的警告:

本次的学习的内容是关于在button动态方法连接多个switch 和textfield
原因是运行时程序将这个方法误认为set get。导致程序不能执行。
本次的学习的内容是关于在button动态方法连接多个switch 和textfield
如果把它改成- (IBAction)onPasswordButtonClick:(id)sender;便没问题了。

代码学习:写一个set 为string:
-(void) setPassword: (NSString){ [password release]; password = nill; self.password = _password;}不返回值-(NSString) getPassword{return password;}

读书人网 >移动开发

热点推荐