搜尋此網誌

2010年4月9日 星期五

iphone開發-型別轉換

// 將字串轉換成int
NSString *intString = @"1";
int value = [intString intValue];

// 將字串轉換成float
NSString *floatString = @"10.00";
float value = [floatString floatValue];

// 將字串轉換成double
NSString *doubleString = @"10.000000";
double value = [doubleString doubleValue];

//將float轉換成字串

float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];

//釋放記憶體

[Text release];

//將int轉換成字串

int weight=5;

NSString* Text = [[NSString alloc] initWithFormat:@"%d", weight];

//釋放記憶體

[Text release];

iphone開發-object與method寫法

在objective C中 我們常可以看到[receiver message]的東西 以下將介紹這個
譬如說 我們產生一個label object
UILabel *label
我們想在label中顯示一段字串 我們可以這麼寫
[label setText:@"This is a test"];
@開頭表示是一串字串 接著用雙引號括號起來
其實它也可以寫成像java 用dot的寫法 這兩個是一樣的
label.text=@"This is a test";
也就是object.method

2010年4月8日 星期四

iphone開發-dismiss the keyboard

今天花了蠻多時間在解決這個問題 在iphone中輸入文字or數字 他會自動彈出虛擬鍵盤 但是輸入完成後 按下return or done(要看你在UI中 設定成什麼) 並不會自動消失 因此需要自行加入某樣東西才行 以下用我今天寫的東西來介紹
(MyViewController.h)
//UIVewController 使用UITextFieldDelegate這個protocol 這樣才能用到使鍵盤消失的method
@interface MyViewController : UIViewController <UITextFieldDelegate> {
//產生一個可以在上面輸入文字的object
UITextField *textField;
UITextField *textField2;
//產生一個label object
UILabel *label;
NSString *string;
NSString *string2;

}
//用IBOulet作為UI的識別
@property (nonatomic,retain)IBOutlet UITextField *textField;
@property (nonatomic,retain)IBOutlet UITextField *textField2;
@property (nonatomic,retain)IBOutlet UILabel *label;
@property (nonatomic,copy)NSString *string;
@property (nonatomic,copy)NSString *string2;
//按鈕宣告
-(IBAction)calculate:(id)sender;
@end

(MyViewController.m)
#import "MyViewController.h"


@implementation MyViewController

@synthesize textField;
@synthesize textField2;
@synthesize label;
@synthesize string;
@synthesize string2;

//按鈕按下要執行的事件
-(IBAction)calculate:(id)sender
{
//將user輸入的文字 放到string中
self.string=textField.text;
self.string2=textField2.text;
float bmi;
float hight;
//string 轉換成 float (ps:這邊我會另外再簡介)
float stringFloat = [string floatValue];
hight=stringFloat/100;
float stringFloat2 = [string2 floatValue];
float hight2=hight*hight;
bmi=stringFloat2/hight2;

if (bmi<18.5) text="@">=18.5&&bmi<24) text="@">=24&&bmi<27) text="@">=27)
{
label.text=@"太胖了..沒救了";
}
}
//這邊就是當user按下虛擬鍵盤的return or done之後 會呼叫此function 接著鍵盤會自動消失
-(Bool)textFieldShouldReturn:(UITextField *)TextField
{
//因為我有兩個textField (也就是可以讓使用者輸入文字的地方)
[textField resignFirstResponder];
[textField2 resignFirstResponder];
return YES;
}

之後就是記得在UI設計那邊 要將兩個textField 連到file's owner 讓file's owner當作代理程式

2010年4月6日 星期二

iphone開發-declare and define(2)

接續上篇所介紹的 今天剛好用到隨機函數 拿這個例子來當作舉例
首先在.h的檔案中 加入下列這行來宣告這個method
-(int)getRandomNumber:(int)from to:(int)to;
接著在.m的檔案中 加入下列這行來定義,也就是告訴他要做些什麼事
-(int)getRandomNumber:(int)from to:(int)to {
//這邊的用法 和C裡面的srand()一樣
return (int)from + arc4random() % (to-from+1);

}

之後在.m裡面 可以加入下列這行來呼叫這個method
//隨機產生1~10
int randomNumber = [self getRandomNumber:1 to:10];



2010年4月1日 星期四

iphone開發-declare and define篇

今天終於把objective c++ K過一遍了 將一些心得寫上來 順便記住一些重點
在開發時 當我新建立一個專案時 我們會看到 XXX.h 和XXX.m 兩個檔案
事實上 開發iphone時 我們會發現 他們將不同功能 不同作用的程式碼 分出來 寫到不同的檔案上
我們先來介紹一下 .h 和.m 主要是在做什麼
"XXX.h" 其實就像是C++ header一樣 在裡面宣告類別以及變數 而objective c++稱作
interface

"XXX.m" 在objective c++裡面稱作implement的部份 也就是定義 該類別要作什麼動作

範例如下:
EX: XXX.h
#import 

@interface Fraction: NSObject {
int numerator;
int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end

EX:XXX.m
#import "Fraction.h"
#import

@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}

-(int) denominator {
return denominator;
}

-(int) numerator {
return numerator;
}
@end