PDA

View Full Version : Text Field Input - Beginner Question




leaf777
Apr 25, 2011, 11:49 PM
Hello,

I am trying to develop an app where a user will be able enter text using the virtual keyboard and then have that data displayed on a label.

I am looking to have the following sequence: The user will access the text field, type a string of characters using the virtual keyboard, and the string of characters will be displayed on a label.

I have the following books "Programming in Objective C 2.0" by Stephen Kochan and "Beginning iOS4 Application Development" by Wei-Meng Lee.

Stephen Kochan's book goes through an exercise where a calculator app is developed. In this app a button for each calculator number corresponds with a label. I have been able to do the beginning parts of this app successfully. I believe I can use this app a stepping point and would like to modify its code for my needs.

To summarize currently I can know how take a preprogrammed string and attach it to a label but how do I allow that string to be entered in by a user using a text field?

Thanks.



The code for the I am using as my base is as follows:

AppDelegate.h file

#import <UIKit/UIKit.h>

@interface iPhone_1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UILabel *display;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *display;

- (IBAction) click1: (id) sender;

@end



AppDelegate.m


#import "iPhone_1AppDelegate.h"

@implementation iPhone_1AppDelegate

@synthesize window, display;

- (void) applicationDidFinishLaunching:(UIApplication *)application {

//overide point for customization after application launch
[window makeKeyAndVisible];
}

-(IBAction) click1: (id) sender

{
[display setText: @"1"];
}



#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

[self.window makeKeyAndVisible];

return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}


- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}


#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 {
[window release];
[super dealloc];
}


@end



jnoxx
Apr 26, 2011, 02:21 AM
Try to avoid the appDelegate, and work with your RootViewController! :)
There, don't forget the UITextFieldDelegate.
Then go to the delegate, and it gives u all the methods u could use, from this delegate.
For example, when u click the return button, that it saves to a label.
So then u can define a Label. and then use the setText or the "labelName.text = @"";
Good luck.

dejo
Apr 26, 2011, 08:50 AM
See Apple's tutorial, "Your First iOS Application" (http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhone101/Articles/00_Introduction.html). It teaches you to do almost exactly what you're asking for.

leaf777
Apr 26, 2011, 09:54 AM
Thank you, I will try those things.

leaf777
Apr 26, 2011, 04:19 PM
Thanks for the links again. I am trying to use apple's "Your First iOS Application" ( http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhone101/Articles/03_AddingViewController.html) but I am encountering some problems.

I think the problem is something trivial but I cannot pinpoint the problem and would greatly appreciate someone's help.

I have 4 issues coming back in debug mode, as follows:

1. error: expected identifier or '(' before '@' token

2. error: '_myViewController' undeclared (first use in this function)

3. warning: property 'myViewController' requires method '-myViewController' to be defined - use @synthesize, @dynamic or provide a method implementation

4. warning: property 'myViewController' requires the method 'setMyViewController:' to be defined - use @synthesize, @dynamic or provide a method implementation




HelloWorldAppDelegate.h

#import <UIKit/UIKit.h>

@class MyViewController;

@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) MyViewController *myViewController;

@end


HelloWorldAppDelegate.m

#import "HelloWorldAppDelegate.h"
#import "MyViewController.h"


@implementation HelloWorldAppDelegate


@synthesize window=_window;

@synthesize myViewController=_myViewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:nil];

self.myViewController = aViewController;

[aViewController release];

self.window.rootViewController = self.myViewController;

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}

- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}

- (void)dealloc
{
[_myViewController release];
[_window release];
[super dealloc];
}

@end

dejo
Apr 26, 2011, 04:46 PM
What does your MyViewController.h look like?

leaf777
Apr 26, 2011, 05:22 PM
MyViewController.h


#import <UIKit/UIKit.h>


@interface MyViewController : UIViewController {

}

@end

dejo
Apr 26, 2011, 05:58 PM
Well, I can't see anything wrong with the code you've supplied.

Which line is giving you this error?:
error: expected identifier or '(' before '@' token

leaf777
Apr 26, 2011, 06:02 PM
It is in the beginning between these two:

@synthesize window=_window;

@synthesize myViewController=_myViewController;

ViviUO
Apr 27, 2011, 04:24 PM
It is in the beginning between these two:

@synthesize window=_window;

@synthesize myViewController=_myViewController;

I don't see why it would be spitting that error at you ... very weird.

Could you upload the project somewhere for me to download?

Sykte
Apr 27, 2011, 07:10 PM
Which version of xcode are you running?