Text Field Input - Beginner Question

Discussion in 'iPhone/iPad Programming' started by leaf777, Apr 25, 2011.

  1. leaf777, Apr 25, 2011
    Last edited by a moderator: Apr 25, 2011

    macrumors newbie

    Joined:
    Apr 25, 2011
    #1
    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:

    Code:
    [B]AppDelegate.h file[/B]
    
    #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
    
    
    
    [B]AppDelegate.m[/B]
    
    
    #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
     
  2. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
  4. thread starter macrumors newbie

    Joined:
    Apr 25, 2011
    #4
    Thank you, I will try those things.
     
  5. thread starter macrumors newbie

    Joined:
    Apr 25, 2011
    #5
    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



    Code:
    [B]HelloWorldAppDelegate.h[/B]
    
    #import <UIKit/UIKit.h>
    
    @class MyViewController;
    
    @interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {
    
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    
    @property (nonatomic, retain) MyViewController *myViewController;
    
    @end
    
    
    [B]HelloWorldAppDelegate.m[/B]
    
    #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
    
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    What does your MyViewController.h look like?
     
  7. thread starter macrumors newbie

    Joined:
    Apr 25, 2011
    #7
    MyViewController.h

    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface MyViewController : UIViewController {
        
    }
    
    @end
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Well, I can't see anything wrong with the code you've supplied.

    Which line is giving you this error?:
    Code:
    error: expected identifier or '(' before '@' token
     
  9. thread starter macrumors newbie

    Joined:
    Apr 25, 2011
    #9
    It is in the beginning between these two:

    @synthesize window=_window;

    @synthesize myViewController=_myViewController;
     
  10. macrumors 6502

    Joined:
    Jul 4, 2009
    #10
    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?
     
  11. macrumors regular

    Joined:
    Aug 26, 2010
    #11
    Which version of xcode are you running?
     

Share This Page