[아이폰 앱 개발] uitextfield

Data Persistence in iPhone

In this application we enter our data to the textfield and save those data to the property list when application quits and reload data again from property list when application launched. So let see how it will be worked.

In this application we enter our data to the textfield and save those data to the property list when application quits and reload data again from property list when application launched. So let see how it will be worked.

Step 1: Create a application using View base application template. Give the application name “DataPersistence”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the DataPersistenceViewController class for you. Expand Resources and notice the template generated a separate nib, DataPersistenceViewController.xib, for the “DataPersistence”.

Step 4: We need to add one property list in the Resource folder.

Step 5: Open the DataPersistenceViewController.h file and we have added two TextField  Outlet ,defined a constant for filename  and  add two methods.So make the following changes in the file.

#import <UIKit/UIKit.h>
#define kPlistname @"Information.plist"

@interface DataPersistanceViewController : UIViewController {

        IBOutlet UITextField *field1;
        IBOutlet UITextField *field2;
}
@property (nonatomic,retain) UITextField *field1;
@property (nonatomic,retain) UITextField *field2;

-(NSString *)dataFilePath;
-(void)applicationWillTerminate:(NSNotification *)notification;

Step 6: Double click the DataPersistenceViewController.xib file and open it to the Interface Builder. First drag two textField from the library and place it to the View window and drag two label from the library and place it to the view window, change the  label names into Your FirstName and Your LastName(See the figure below). Connect the File’s Owner icon to the View icon select view, connect File’s Owner icon to the first TextField and select field1. Do it same for next TextField and select field2. Now save the DataPersistenceViewController.xib file, close it and go back to the Xcode.

Step 7: In the DataPersistenceViewController.m file make the following changes :

-(NSString *)dataFilePath
{
   NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
   NSString *documentsDirectory = [path objectAtIndex:0];
   return [documentsDirectory stringByAppendingPathComponent:kPlistname];
}

-(void)applicationWillTerminate:(NSNotification *)notification
{
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:field1.text];
        [array addObject:field2.text];
        [array writeToFile:[self dataFilePath] atomically:YES];
        [array release];
}

- (void)viewDidLoad {

        NSString *filePath =[self dataFilePath];
        if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
        {
                NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
                field1.text = [array objectAtIndex:0];
                field2.text = [array objectAtIndex:1];
                [array release];
                
                UIApplication *app = [UIApplication sharedApplication];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) 
                                                                                                   name:UIApplicationWillTerminateNotification object:app];
        }
                
     [super viewDidLoad];
}

Step 8: Now compile and run the application in simulator. But remember you should type something in the textfield and press the Home button, otherwise if you exit the application, then your data will not save.

You can download source code from here DataPersistance


http://edumobile.org/iphone/iphone-programming-tutorials/data-persistence-in-iphone/

Posted by 오늘마감

댓글을 달아 주세요

how to make a UITextField move up when the keyboard is presented


아래 답변대로 하되 매크로설정빼먹으면 오류~ 매크로수치만큼 뷰가 위로올라간다. 뷰가 움직이는 것이 감지되나 여전히 가리면 아래 매크로값을 조정하면 된다.

일반 뷰에 텍스트필드가 있을때에도 가능하다.



#define kOFFSET_FOR_KEYBOARD 120.0

With the iPhone SDK:

I have a UIView with UITextFields that brings up a keyboard. I need it to be able to:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up
  2. Automatically "jump" (by scrolling up) or shortening

I know that I need a UIScrollView. I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down.

Do I need both a UIView and a UIScrollView? Does one go inside the other? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then what needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.


http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present


Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. I'm simply using: 


-(void)textFieldDidBeginEditing:(UITextField *)textField { //Keyboard becomes visible scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y,                                                    scrollView.frame.size.width, scrollView.frame.size.height - 215 + 50); //resize } 
-(void)textFieldDidEndEditing:(UITextField *)textField { //keyboard will hide scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + 215 - 50); //resize }
However this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.
flag

12 Answers

9

1) You will need a scroll view if the contents you have now does not fit in iphone screen. [If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then its not needed]

2) For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown

here is a sample code


-(void)textFieldDidBeginEditing:(UITextField*)sender
{
if([sender isEqual:_textField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >=0)
{
[self setViewMovedUp:YES];
}
}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];// if you want to slide up the view

CGRect rect =self.view.frame;
if(movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect
.origin.y -= kOFFSET_FOR_KEYBOARD;
rect
.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect
.origin.y += kOFFSET_FOR_KEYBOARD;
rect
.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;

[UIView commitAnimations];
}


-(void)keyboardWillShow:(NSNotification*)notif
{
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

if([_textField isFirstResponder]&&self.view.frame.origin.y >=0)
{
[self setViewMovedUp:YES];
}
elseif(![_textField isFirstResponder]&&self.view.frame.origin.y <0)
{
[self setViewMovedUp:NO];
}
}


-(void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name
:UIKeyboardWillShowNotificationobject:self.view.window];
}

-(void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
}

define kOFFSET_FOR_KEYBOARD to a value as needed. like


#define kOFFSET_FOR_KEYBOARD 60.0

Hope this helps




Posted by 오늘마감

댓글을 달아 주세요

how to make a UITextField move up when the keyboard is presented

With the iPhone SDK:

I have a UIView with UITextFields that brings up a keyboard. I need it to be able to:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up
  2. Automatically "jump" (by scrolling up) or shortening

I know that I need a UIScrollView. I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down.

Do I need both a UIView and a UIScrollView? Does one go inside the other? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then what needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present


Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. I'm simply using: 

-(void)textFieldDidBeginEditing:(UITextField *)textField { //Keyboard becomes visible scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 							 scrollView.frame.size.width, scrollView.frame.size.height - 215 + 50); //resize } 

-(void)textFieldDidEndEditing:(UITextField *)textField { //keyboard will hide scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + 215 - 50); //resize }

However this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.
flag

   

12 Answers

9

1) You will need a scroll view if the contents you have now does not fit in iphone screen. [If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then its not needed]

2) For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown

here is a sample code

-(void)textFieldDidBeginEditing:(UITextField*)sender
{
   
if([sender isEqual:_textField])
   
{
       
//move the main view, so that the keyboard does not hide it.
       
if  (self.view.frame.origin.y >=0)
       
{
           
[self setViewMovedUp:YES];
       
}
   
}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
   
[UIView beginAnimations:nil context:NULL];
   
[UIView setAnimationDuration:0.5];// if you want to slide up the view

   
CGRect rect =self.view.frame;
   
if(movedUp)
   
{
       
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
       
// 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect
.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect
.size.height += kOFFSET_FOR_KEYBOARD;
   
}
   
else
   
{
       
// revert back to the normal state.
        rect
.origin.y += kOFFSET_FOR_KEYBOARD;
        rect
.size.height -= kOFFSET_FOR_KEYBOARD;
   
}
   
self.view.frame = rect;

   
[UIView commitAnimations];
}


-(void)keyboardWillShow:(NSNotification*)notif
{
   
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

   
if([_textField isFirstResponder]&&self.view.frame.origin.y >=0)
   
{
       
[self setViewMovedUp:YES];
   
}
   
elseif(![_textField isFirstResponder]&&self.view.frame.origin.y <0)
   
{
       
[self setViewMovedUp:NO];
   
}
}


-(void)viewWillAppear:(BOOL)animated
{
   
// register for keyboard notifications
   
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
                                                                                         name
:UIKeyboardWillShowNotificationobject:self.view.window];
}

-(void)viewWillDisappear:(BOOL)animated
{
     
// unregister for keyboard notifications while not visible.
   
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
}

define kOFFSET_FOR_KEYBOARD to a value as needed. like

#define kOFFSET_FOR_KEYBOARD 60.0

Hope this helps



출처 : http://blog.naver.com/PostList.nhn?blogId=philipousys¤tPage=41
Posted by 오늘마감

댓글을 달아 주세요

아이폰 UITextField 키보드 핸들링 소스

iPhone UITextField Tutorial: Handling Keyboard Interactions

This post is written to provide you with the process to get the return button working on the keypad that pops up while filling a text field, the “Background Tap” functionality, and also what to do when the text field hides behind the keypad.

First we make a demo View-based Application for this, say “BackgroundTapForBlog” .
Add a text field to BackgroundTapForBlogViewController.xib, declare it in your BackgroundTapForBlogViewController.h file ,say tfUsername and link them in .xib.

Return Button on KeyPad:

Once you are done with this, add a function “textFieldDoneEditing” to the BackgroundTapForBlogViewController.m file, and do not forget to declare it in the BackgroundTapForBlogViewController.h file. This function gets rid of the keypad once you are done filling in the textfield.

BackgroundTapForBlogViewController.h file -

1 #import
2 @interface BackgroundTapForBlogViewController: UIViewController{
3 UITextField *tfUsername;
4 }
5 @property(nonatomic, retain) IBOutlet UITextField *tfUsername;
6 -(IBAction) textFieldDoneEditing : (id) sender;
7 -(IBAction) backgroundTap:(id) sender;
8 @end

BackgroundTapForBlogViewController.m file -

01 #import "BackgroundTapForBlogViewController.h"
02  
03 @implementation BackgroundTapForBlogViewController
04 @synthesize tfUsername;
05  
06 - (void)viewDidUnload {
07 self.tfUsername = nil;
08 }
09  
10 - (void)dealloc {
11 [tfUsername release];
12 [super dealloc];
13 }
14  
15 -(IBAction) textFieldDoneEditing : (id) sender{
16 [sender resignFirstResponder];
17 }
18  
19 -(IBAction) backgroundTap:(id) sender{
20 [self.tfUsername resignFirstResponder];
21 }
22 @end

You can download the source code from here.

http://www.mobisoftinfotech.com/blog/iphone/iphone-uitextfield-tutorial-handling-keyboard-interactions/http://www.mobisoftinfotech.com/blog/iphone/iphone-uitextfield-tutorial-handling-keyboard-interactions/

Posted by 오늘마감

댓글을 달아 주세요