我正在尝试为自己编写一个小体重观察者积分计算器应用程序。我知道你可以从 appstore 下载一个,但我正在尝试学习 Objective-c,并认为这将是一个相当简单的第一个应用程序。我只想使用输入到文本框中的文本并将其保存为变量并使用所述变量通过 intValues 执行基本数学运算。我试过使用 UITextField 的“文本”属性但没有成功,有人可以帮忙告诉我我在这里做错了什么吗?好吧,这很尴尬……看来我忘了告诉你问题出在哪里。我已经将我收到的错误包含在相应的行中。我从这些错误中得到的是我不应该使用“NSString”来定义我的变量,也许是“int”?
.h 文件:
#import <UIKit/UIKit.h>
@class FirstViewController;
NSString*txtProtein = nil;
NSString *txtCarbs = nil;
NSString *txtFat = nil;
NSString *txtFiber = nil;
NSString *txtPoints = nil;
NSString *txtproteinCalc = nil;
NSString *txtcarbCalc = nil;
NSString *txtfatCalc = nil;
NSString *txtfiberCalc = nil;
@interface AppDelegate : NSObject
<UIApplicationDelegate> {
UIWindow *window;
FirstViewController
*viewController;
UITextField *protein;
UITextField *carbs;
UITextField *fat;
UITextField *fiber;
UITextView *points;
}
@property (nonatomic, retain)
IBOutlet UIWindow *window;
@property (nonatomic, retain)
IBOutlet FirstViewController *viewController;
@end
和 .m 文件:
#import "FirstViewController.h"
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Here is where all of the textboxes are defined.
//All of the strings are in the viewController.h
//This is the protein part
txtProtein = [[[UITextField alloc] initWithFrame:CGRectMake(10.0, 10.0, 50.0, 25.0)]; (expected identifier)
txtProtein.backgroundColor = [UIColor whiteColor]; (property "backgroundcolor" not found on object of type 'NSString')
txtProtein.placeholder = @"Protein"; (property "placeholder" not found on object of type 'NSString')
[viewController.view addSubview:txtProtein]; (property 'view' can not be found in forward class object "FirstViewController")
//This is the carbs part
txtCarbs = [[[UITextField alloc] initWithFrame:CGRectMake(30.0, 10.0, 50.0, 25.0)];
txtCarbs.backgroundColor = [UIColor whiteColor];
txtCarbs.placeholder = @"Carbs";
[viewController.view addSubview:txtCarbs];
//This is the Fat part
txtFat = [[[UITextField alloc] initWithFrame:CGRectMake(50.0, 10.0, 50.0, 25.0)];
txtFat.backgroundColor = [UIColor whiteColor];
txtFat.placeholder = @"Fat";
[viewController.view addSubview:txtFat];
//This is the Fiber
txtFiber = [[[UITextField alloc] initWithFrame:CGRectMake(70.0, 10.0, 50.0, 25.0)];
txtFiber.backgroundColor = [UIColor whiteColor];
txtFiber.placeholder = @"Fiber";
[viewController.view addSubview:txtFiber];
//Total Points
txtPoints = [[[UITextField alloc] initWithFrame: CGRectMake(150.0, 10.0, 50.0, 25.0)]autorelease];
txtPoints.backgroundColor = [UIColor greenColor];
txtPoints.editable = NO;
[viewController.view addSubview:txtPoints];
//Protein divided by 10.9375
txtproteinCalc = [txtProtein.text intValue] / [10.9375];
//Carbs divided by 9.2105
txtcarbCalc = [txtCarbs.text intValue] / [9.2105];
//Fat divided by 3.8889
txtfatCalc = [txtFat.text intValue] / [3.8889];
//Fiber divided by 12.5
txtfiberCalc = [txtFiber.text intValue] / [12.5];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(calculatePoints)
name:UITextFieldTextDidChangeNotification
object:nil];
//Add the view controller's view to the window and display
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
//Make keyboard show up in the Protein (The first) box
[txtProtein becomeFirstResponder];
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much Memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later/
*/
}
-(void)dealloc {
[txtProtein release];
[txtFiber release];
[txtFat release];
[txtCarbs release];
[txtPoints release];
[viewController release];
[window release];
[super dealloc];
}
#pragma mark -
#pragma mark Other Methods
-(void)calculatePoints {
NSLog(@"Calculating FoodTrakr Value...");
txtPoints.text = @"";
if (txtProtein.text.length > 0 && [txtProtein.text intValue]>0 && txtFiber.text.length >0 && [txtFiber.text intValue]>0 && txtFat.text.length >0 && [txtFat.text intValue]>0 && txtCarbs.text.length >0 && [txtCarbs.text intValue]>0
)
{
int Points = [txtproteinCalc.text intValue] + [txtcarbCalc.text intValue] + [txtfatCalc.text intValue] - [txtfiberCalc.text intValue];
txtPoints.text = [[NSNumber numberWithInt:points] stringValue];
}
}
@end
提前感谢您的帮助!另外,如果你们有任何推荐的用于学习目标 c 的书籍,我已经阅读了一些书,但我知道我显然需要阅读更多内容,如果你能留下任何帮助你学习的建议,那就太棒了。谢谢!