这次的学习的内容是关于在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 (nonatomicretainUISwitch *includeUpperCase;


@property (nonatomicretainUISwitch *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 = @"";

    //定义az字母

    NSString *lowercaseChars = @"abcdefghijklmnopqrstuvwxyz";

    //定义AZ字母

    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




运行程序:

当把所有字母大写小写和数字开启时。

当只把字母大写关闭时。

当把数字开启时。

总结:
@property  ( nonatomic ,  retain )   UITextField   *password;

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


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


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

2012年07月11日
原因是运行时程序将这个方法误认为set get。导致程序不能执行。


如果把它改成 - ( IBAction )onPasswordButtonClick:( id )sender;便没问题了。


代码学习:
写一个set 为string:

-(void) setPassword: (NSString)
{
     [password release];
     password = nill;
     self.password = _password;
}
不返回值
-(NSString) getPassword
{
return password;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值