私は自分自身のために、ちょっとした weightwatchers ポイント計算アプリを書こうとしています。アプリストアからダウンロードできることは知っていますが、Objective-C を学習しようとしていて、これは最初のアプリとしてはかなり簡単だと思いました。テキストボックスに入力されたテキストを使用して変数として保存し、その変数を使用してintValuesで基本的な計算を実行したいだけです。UITextField の 'text' プロパティを使ってみましたがうまくいきませんでした。うーん、これは厄介です...何が問題なのかをお伝えするのを忘れていたようです。対応する行に受け取ったエラーを含めました。これらのエラーから得られるのは、「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 を学習するための推奨本があれば、私はいくつか読んだことがありますが、明らかにもっと読む必要があることはわかっています。ありがとう!