-2

我正在尝试为自己编写一个小体重观察者积分计算器应用程序。我知道你可以从 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 的书籍,我已经阅读了一些书,但我知道我显然需要阅读更多内容,如果你能留下任何帮助你学习的建议,那就太棒了。谢谢!

4

1 回答 1

1

坦率地说,发布的代码有很多严重的问题。让我们从直接的编译错误开始。

你有:

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")

第一行有一个太多的左括号。另一个主要问题是您正在创建 aUITextField但您将其分配给 a 的变量NSString。而不是txtProtein您应该使用protein具有属性类型的变量。所以代码应该是:

protein = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 10.0, 50.0, 25.0)];
protein.backgroundColor = [UIColor whiteColor];
protein.placeholder = @"Protein";
[viewController.view addSubview:protein];

您在此代码中一遍又一遍地重复相同的问题。

下一个大问题是为什么所有这些代码都在您的应用程序委托中?大多数(如果不是全部)代码都属于该FirstViewController.m文件。您的应用程序委托不应该负责设置视图控制器。让视图控制器自行设置。

接下来,为什么你有所有这些全局变量?

我建议你先找到一些关于 Objective-C 编程的好教程,然后再找到一些关于 iOS 应用程序开发的好教程。

于 2013-01-12T01:12:02.517 回答