這次的學習的內容是關於在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;
}

發佈了44 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章