這幾天寫了一個可以計算你一天吃多少熱量之後 一個月後會瘦幾公斤 或變胖
有用到UISlider 剛好拿來記錄一下
"MyViewController.h"
@interface MyViewController : UIViewController {
UITextField *textfield;
UITextField *textfield2;
UITextField *textfield3;
UITextField *textfield4;
UILabel *display;
UILabel *display2;
//產生一個UISlider object
UISlider *slider;
NSString *string;
NSString *string2;
NSString *string3;
NSString *string4;
}
@property(nonatomic,retain)IBOutlet UITextField *textfield;
@property(nonatomic,retain)IBOutlet UITextField *textfield2;
@property(nonatomic,retain)IBOutlet UITextField *textfield3;
@property(nonatomic,retain)IBOutlet UITextField *textfield4;
//告訴IB有一個UISlider
@property(nonatomic,retain)IBOutlet UISlider *slider;
@property(nonatomic,retain)IBOutlet UILabel *display;
@property(nonatomic,retain)IBOutlet UILabel *display2;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,copy)NSString *string2;
@property(nonatomic,copy)NSString *string3;
@property(nonatomic,copy)NSString *string4;
-(IBAction)calculate:(id)sender;
-(IBAction)reset:(id)sender;
//對應UISlider的event
-(IBAction)sliderChanged:(id)sender;
-(IBAction)female:(id)sender;
-(IBAction)male:(id)sender;
@end
"MyViewController.m"
@implementation MyViewController
@synthesize textfield;
@synthesize textfield2;
@synthesize textfield3;
@synthesize textfield4;
@synthesize slider;
@synthesize display;
@synthesize display2;
@synthesize string;
@synthesize string2;
@synthesize string3;
@synthesize string4;
float a;
int characterselect;
//移動slider的event
-(IBAction)sliderChanged:(id)sender
{
NSString *newText;
UISlider *slider=(UISlider *)sender;
//先在IB裡面設定範圍 此處我設定為0~4 user移動slider之後 就會是0~4其中一個
int progressAsInt=(int)(slider.value+0.5f);
switch (progressAsInt) {
case 0:
{
newText=@"很少運動或完全沒運動";
a=1.2;
break;
}
case 1:
{
newText=@"每周運動1-3次";
a=1.375;
break;
}
case 2:
{
newText=@"每周運動3-5次";
a=1.55;
break;
}
case 3:
{
newText=@"每周運動6-7次";
a=1.725;
break;
}
case 4:
{
newText=@"每天非常重度的運動或體力勞動者";
a=1.9;
break;
}
default:
newText=@"exception error";
break;
}
//在diplay2這個label顯示字串
display2.text=newText;
[newText release];
}
-(IBAction)calculate:(id)sender
{
self.string=textfield.text;
self.string2=textfield2.text;
self.string3=textfield3.text;
self.string4=textfield4.text;
//將字串轉成int
int stringint=[string intValue];
int stringint2=[string2 intValue];
int stringint3=[string3 intValue];
int stringint4=[string4 intValue];
if (characterselect==0) {
//基礎代謝率公式
float kg=9.6*stringint2;
float cm=1.8*stringint;
float age=4.7*stringint3;
float benefit=655+kg+cm-age;
float result=a*benefit;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
if (characterselect==1) {
float kg=13.8*stringint2;
float cm=5.0*stringint;
float age=6.8*stringint3;
float benefit=66+kg+cm-age;
float result=a*benefit;
//浮點數轉成字串
//NSString* myNewString = [[NSString alloc] initWithFormat:@"%f", a];
//display.text=myNewString;
float result2=result-stringint4;
if (result2<0) {
display.text=@"只會變胖不會變瘦";
}
if (result2==0) {
display.text=@"不會變胖也不會變瘦";
}
if(result2>0)
{
float benefit2=result2*30;
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//字串結合
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
display.text=ConText;
[Text release];
}
}
}
//按鈕按下去會觸發的event
-(IBAction)female:(id)sender
{
characterselect=0;
}
-(IBAction)male:(id)sender
{
characterselect=1;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
[textfield resignFirstResponder];
[textfield2 resignFirstResponder];
[textfield3 resignFirstResponder];
[textfield4 resignFirstResponder];
return YES;
}
-(IBAction)reset:(id)sender
{
textfield.text=@"";
textfield2.text=@"";
textfield3.text=@"";
textfield4.text=@"";
display.text=@"";
display2.text=@"";
}
- (void)dealloc {
[textfield release];
[textfield2 release];
[textfield3 release];
[string release];
[string2 release];
[string3 release];
[display release];
[slider release];
[super dealloc];
}
@end
沒有留言:
張貼留言