目標:
這次的學習的內容是關於在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 運行程序: