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



Clound SSD Virtual Server

iOS/iPhone Textbox in Popup - UITextField / UIAlertView (Objective-C,iPhone)

iOS/iPhone Textbox in Popup - UITextField and UIAlertView (Objective-C,iPhone,iPad) การสร้าง Textbox (UITextField) บน Dialog Popup (UIAlertView) ใช้สำหรับการรับค่าจากผู้ใช้ อาจจะเป็นข้อความสั้น ๆ เช่น ชื่อ หรือ อีเมล์ หรือข้อมูลอื่น ๆ ซึ่งวิธีการทำนั้นก็ไม่ได้ยากอะไรมากมาย และยิ่งใช้ iOS5 แล้วก็จะสามารถทำได้ง่ายขึ้นมาก


iOS/iPhone Textbox in Popup

Textbox in Popup - UITextField / UIAlertView


ในการสร้าง Text Field หรือ UITextField สำหรับกรอกข้อมูลบน บน Popup เราจะใช้ Class ของ UIAlertView ซึ่งจะใช้การสร้าง Object ของ UITextField ขึ้นมา และทำการ Add เข้าไปใน UIAlertView แต่ใน iOS5 เป็นต้นไปจะสามารถทำได้ง่ายกกว่านั้น ซึ่งจะได้เขียนไว้ในบทความถัดไป

    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
    [alert setTransform:myTransform];
    
    
    UITextField *myTextField = [[UITextField alloc]
                                initWithFrame:CGRectMake(12, 45, 260, 25)];
    [myTextField setTag:10251];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [alert addSubview:myTextField];

การอ่านค่า

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField* myField = (UITextField*)[alertView viewWithTag:10251];
    lblResult.text = [myField text];
}


ทดสอบจริงบน Xcode กับ iPhone

iOS/iPhone Textbox in Popup

เริ่มต้นการสร้าง Project เลือก Single View Application แบบง่าย ๆ

iOS/iPhone Textbox in Popup

กำหนดชื่อ Project

iOS/iPhone Textbox in Popup

สร้าง Interface ดังรูป โดยประกอบด้วย Button และ Label และทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อยด้วย








สำหรับ Code ของ .h และ .m

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *lblResult;
}

- (IBAction)btnPopup:(id)sender;

@end

ViewController.m
//
//  ViewController.m
//
//  Created by Weerachai on 10/27/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.
}

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

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

- (IBAction)btnPopup:(id)sender {
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Enter Your Name."
                          message:@"Hi!"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"OK!", nil];
    
    
    
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
    [alert setTransform:myTransform];
    
    
    UITextField *myTextField = [[UITextField alloc]
                                initWithFrame:CGRectMake(12, 45, 260, 25)];
    [myTextField setTag:10251];
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [alert addSubview:myTextField];
    
    
    [alert show];
    [alert release];
    [myTextField release];
    
}

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UITextField* myField = (UITextField*)[alertView viewWithTag:10251];
    lblResult.text = [myField text];
}

@end


Screenshot

iOS/iPhone Textbox in Popup

คลิกที่ Button เพื่อเปิด Dialog Popup

iOS/iPhone Textbox in Popup

แสดง Dialog Popup และช่อง Text Box (UITextField) สำหรับกรอกข้อมูล

iOS/iPhone Textbox in Popup

การแสดงข้อมูลที่ได้จาก Popup

สำหรับ iOS5 เป็นต้นไป จะสามารถทำได้ง่าย ๆ กว่านี้ ลองอ่านบทความนี้ดูครับ

iOS/iPhone Username and Password Alert Popup - UIAlertView (Objective-C,iPhone,iPad)








.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-29 15:14:02 / 2017-03-25 22:43:36
  Download : Download  iOS/iPhone Textbox in Popup - UITextField / UIAlertView (Objective-C,iPhone)
 Sponsored Links / Related

 
การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Confirm Dialog Popup - UIAlertView (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Dialog Popup and Optional Item - UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Username and Password Popup - UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Action Sheet Popup - UIActionSheet (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Adding Show Image Action Sheet (UIActionSheet) and Alert View (UIAlertView)
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 04
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 อัตราราคา คลิกที่นี่