Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > [iOS/iPhone] Tutorials - สอนเขียน iPhone App ฟรี เขียน iPad App เรียน iPhone เขียนโปรแกรม iPhone > iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)



Clound SSD Virtual Server

iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)

iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number) ในการเขียน iOS เพื่อรับค่า Input ต่าง ๆ เช่น Text Fields หรือ Textbox เราสามารถที่จะ Validate Keyboard ได้ เช่น ถ้าเป็นตัวเลข Number ก็ให้แสดงเฉพาะตัวเลข หรือประเภท หมายเลขโทรศัพท์ (Phone No) , URL เว็บไซต์ ทั้งนี้เพื่อความสะดวกแก้ผู้ใช้ที่จะทำการ Input ข้อมูล รวมทั้งจะได้ทำการตรวจสอบ Validate ข้อมูลเบื้องต้นอีกด้วย

iOS/iPhone Hide Input Keyboard and Validate Text Field

iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)


ในตัวอย่างนี้จะสอนวิธีการ Validate Text Field หรือ TextBox แบบง่าย ๆ โดยใช้ความสามารถของ Object Text Fields เอง และก็จะมีตัวอย่างการซ่อน Hide Input Keyboard เมื่อคลิกที่ตำแหน่งอื่น ๆ ของ View

iOS Hide Input Keyboard
[txtName resignFirstResponder];


Example 1 การ Validate Text Field กับ Input Keyboard และการซ้อน Hide Input Keyboard

iOS/iPhone Hide Input Keyboard and Validate Text Field

เริ่มต้นด้วยการสร้าง Application บน Xcode ง่าย ๆ แบบ Single View Application

iOS/iPhone Hide Input Keyboard and Validate Text Field

เลือกและไม่เลือกดังรูป

iOS/iPhone Hide Input Keyboard and Validate Text Field

ตอนนี้หน้าจอ View จะยังว่าง ๆ ดังรูป

iOS/iPhone Hide Input Keyboard and Validate Text Field

ออกแบบหน้าจอ View ง่าย ๆ ดังรูป

iOS/iPhone Hide Input Keyboard and Validate Text Field

กรณีที่เป็น Text Fields แบบรหัสผ่าน (Password) ให้เลือก Secure








iOS/iPhone Hide Input Keyboard and Validate Text Field

ในการที่จะ Validate Input Keyboard ให้กำหนดคุณสมบัติของแต่ล่ะ Text Field ได้ทันที โดยคลิกที่ Keyboard

iOS/iPhone Hide Input Keyboard and Validate Text Field

เลือกรูปแบบต่าง ๆ ตามต้องการ เช่น Number , Phone , URL , E-Mail หรืออื่น ๆ ขึ้นอยู่กับชนิดข้อมูลที่ต้องการ

Screenshot

iOS/iPhone Hide Input Keyboard and Validate Text Field

แบบ Password ที่เป็น Secure

iOS/iPhone Hide Input Keyboard and Validate Text Field

แบบ Number

iOS/iPhone Hide Input Keyboard and Validate Text Field

แบบ URL

iOS/iPhone Hide Input Keyboard and Validate Text Field

แบบ E-Mail

iOS/iPhone Hide Input Keyboard and Validate Text Field

แบบ Phone No

Example 2 การซ่อนหรือ Hide Input Keyboard หลังจากที่ได้คลิกในตำแหน่งอื่น ๆ ของ View

iOS/iPhone Hide Input Keyboard and Validate Text Field

ใน Class ของ.h ให้เชื่อม IBOutlet ดังรูป

iOS/iPhone Hide Input Keyboard and Validate Text Field

ในการที่เราจะคลิกตำแหน่งอื่น ๆ ของ View แล้วเขียน Handle นั้นเราจะต้องอาศัย Gesture เข้ามาช่วย โดยใช้ Gesture ที่มีชื่อว่า UITapGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // Tap Gesture for Hide Keyboard
    UITapGestureRecognizer *oneTapGesture = [[UITapGestureRecognizer alloc]
                                             initWithTarget: self
                                             action: @selector(hideKeyboard:)];
    [oneTapGesture setNumberOfTouchesRequired:1];
    [[self view] addGestureRecognizer:oneTapGesture];
}

// Event Gesture for Hide Keyboard
- (void)hideKeyboard:(UITapGestureRecognizer *)sender {
    [txtPassword resignFirstResponder];
    [txtNumber resignFirstResponder];
    [txtURL resignFirstResponder];
    [txtEmail resignFirstResponder];
    [txtPhone resignFirstResponder];
}



Screenshot

iOS/iPhone Hide Input Keyboard and Validate Text Field

เมื่อคลิกที่ Text Fields หรือ TextBox จะแสดง Input Keyboard

iOS/iPhone Hide Input Keyboard and Validate Text Field

เมื่อคลิกที่ตำแหน่งอื่น ๆ บน View ตัว Input Keyboard จะหายไป

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

ViewController.h
//
//  ViewController.h
//  inputTextFieldsKeyboard
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    
    IBOutlet UITextField *txtPassword;
    
    IBOutlet UITextField *txtNumber;
    
    IBOutlet UITextField *txtURL;
    
    IBOutlet UITextField *txtEmail;
    
    IBOutlet UITextField *txtPhone;
    
}

@end









ViewController.m
//
//  ViewController.m
//  inputTextFieldsKeyboard
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // Tap Gesture for Hide Keyboard
    UITapGestureRecognizer *oneTapGesture = [[UITapGestureRecognizer alloc]
                                             initWithTarget: self
                                             action: @selector(hideKeyboard:)];
    [oneTapGesture setNumberOfTouchesRequired:1];
    [[self view] addGestureRecognizer:oneTapGesture];
}

// Event Gesture for Hide Keyboard
- (void)hideKeyboard:(UITapGestureRecognizer *)sender {
    [txtPassword resignFirstResponder];
    [txtNumber resignFirstResponder];
    [txtURL resignFirstResponder];
    [txtEmail resignFirstResponder];
    [txtPhone resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [txtPassword release];
    [txtNumber release];
    [txtURL release];
    [txtEmail release];
    [txtPhone release];
    [super dealloc];
}
@end


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-12 08:50:14 / 2017-03-26 09:47:19
  Download : Download  iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)
 Sponsored Links / Related

 
iOS/iPhone Collection View and Master Detail (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView) (iPhone, iPad)
Rating :

 
iOS/iPhone AD BannerView (iAd Framework)
Rating :

 
iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)
Rating :

 
iOS/iPhone Search Data from Web Server (URL,Website)
Rating :

 
iOS/iPhone Edit Update Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Delete Remove Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Register Form and Send Data to Web Server (PHP & MySQL)
Rating :

 
iOS/iPhone Login Username and Password from Web Server (PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่