当前位置:编程学习 > wap >>

这次的学习的内容是关于在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
 
 
 
运行程序:

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,