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 > การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad)



Clound SSD Virtual Server

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

การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad) ใน iOS ที่ทำงานบน iPhone และ iPad ได้มีการยอมรับว่า Interface ที่เป็น Alert หรือ Message Box ที่สวยงาม และในบทความนี้เราจะมาลองสร้าง กล่องโต้ตอบกับผู้ใช้แบบง่าย ๆ ก้วยการให้ผู้ใช้กรอกชื่อลงไปใน Text Field หรือ Textbox และมัปุ่ม Button ให้ผู้ใช้คลิก หลังจากคลิกแล้วเราจะแสดงข้อความตอบกลับสวัสดีกับผู้ใช้ทาง Alert Message และบน Label ที่อยู่ในหน้าจอ

iOS Message Box Alert iPhone iPad

iOS Alert Message Box for iPhone and iPad


สำหรับวิธีเขียน Objective-C ในการสร้าง Alert หรือ Alert Message Box บน iPhone หรือ iPad สามารถสร้างและแทรกคำสั่งในการแสดงผลบนหน้าจอ App ได้ง่าย ๆ สั้น ๆ ด้วยคำสั่งนี้

iOS UIAlertView
UIAlertView *welcomemessage =[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [welcomemessage show];

รูปแบบการ Alert MessageBox ด้วย UIAlertView

ถ้ายังไม่มีพื้นฐานการเขียน Objective-C กับ IBOutlet แนะนำให้อ่าน 4 บทความนี้ก่อน


เริ่มต้นด้วยการสร้าง Project บน Xcode ในตอนนี้ยังเห็นว่ายังเป็นหน้าจอว่างเปล่า

iOS Message Box Alert iPhone iPad

หน้าจอว่างเปล่าของ Interface Builder (IB)

iOS Message Box Alert iPhone iPad

ลาก Object ต่าง ๆ มาไว้ที่ Interface Builder ประกอบด้วย Label , Text Field , Round Rect Button จัดรูปแบบตำแหน่งตามต้องการ








วิธีการสร้าง IBOutlet และ IBAction เปิดหน้าจอ Xcode ให้อยู่ในรูปแบบ Layout ดังรูป ด้วยการคลิกที่ Editor ดูภาพประกอบ

iOS Message Box Alert iPhone iPad

ให้อยู่ในมุมมองของ ViewController.xib และ ViewController.h ซึ่งตอนนี้เราจะทำการสร้าง IBOutlet และ IBAction ให้กับไฟล์ ViewController.h

iOS Message Box Alert iPhone iPad

จะสังเกตุว่าตอนนี้ในไฟล์ ViewController.h ยังไม่มีอะไร นอกจากที่เป็นค่า Default ที่โปรแกรมให้มา

iOS Message Box Alert iPhone iPad

จัดการใส่ปีกกา เปิด และ ปิด {....} ดูภาพประกอบ

ให้เปิดไฟล์ ViewController.h

iOS Message Box Alert iPhone iPad

ขั้นแรกจะสร้าง IBOutlet สำหรับ Text Field ให้กดคลิกที่ Text Field ที่อยู่ในหน้าจอ Design จากนั้นให้คลิกปุ่ม Control ค้างไว้ แล้วลาก Text Field ไปไว้ใน ViewController.h ในตำแหน่งที่อยู่ในปีกกาดังรูป

iOS Message Box Alert iPhone iPad

ตั้งชื่อ IBOutlet ของ Text Field เป็น txtName

iOS Message Box Alert iPhone iPad

ได้ IBOutlet ชนิด UITextField ชื่อว่า txtName ที่เชื่อมกับ Text Field ในหน้าจอ Interface Builder เรียบร้อย ง่าย ๆ

iOS Message Box Alert iPhone iPad

ต่อไปจะเป็น Label ทำเหมือนกัน

iOS Message Box Alert iPhone iPad

กำหนดชื่อเป็น lblResponse

iOS Message Box Alert iPhone iPad

ได้ IBOutlet ชนิด UILabel ชื่อว่า lblResponse

iOS Message Box Alert iPhone iPad

ต่อไปจะเป็น Button ซึ่งตัวนี้เราจะกำหนดเป็น IBAction เพื่อควบคุมการคลิก หรือทำงานบน Button นี้ โดยลากไว้ในตำแหน่งหลังจากปิดปีกกาแล้ว

เปิดที่ไฟล์ ViewController.m

iOS Message Box Alert iPhone iPad

เลือก Connection เป็นแบบ Action

iOS Message Box Alert iPhone iPad

กำหนด Connection เป็นแบบ Action พร้อมกับกำหนดชื่อว่า Hello

iOS Message Box Alert iPhone iPad

เราจะได้ Method ที่เป็น Event ชื่อว่า Hello เรียบร้อย พร้อม ๆ กับการเชื่อมการทำงานกับ Button ที่อยู่ในหน้าจอ Interface Builder (IB)

iOS Message Box Alert iPhone iPad

เลื่อนเมาส์ไปข้างบนเพื่อจะเปลี่ยนมุมมองไปยังไฟล์ ViewController.m








iOS Message Box Alert iPhone iPad

คลิกเลือก ViewController.m เพื่อจะไปเขียน Event ในไฟล์นี้

iOS Message Box Alert iPhone iPad

จะเห็นว่าที่ไฟล์ ViewController.m มีการสร้าง IBAction ของ Button ที่ชื่อว่า Hello ให้เรียบร้อยแล้ว โดยที่เราไม่ต้องมานั่งพิมพ์เอง

iOS Message Box Alert iPhone iPad

เพิ่มคำสั่งสำหรับแสดงผลง่าย ๆ ในคำสั่งนี้

- (IBAction)Hello:(id)sender {
    
    NSString *hello = @"สวัสดีครับคุณ ";
    NSString *name = txtName.text;
    NSString *message;
    message = [hello stringByAppendingString:name];
    
    lblResponse.text = message;
    
    
    UIAlertView *welcomemessage =[[UIAlertView alloc] initWithTitle:hello message:name delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [welcomemessage show];
    
}


ทดสอบการทำงาน

iOS Message Box Alert iPhone iPad

แสดงข้อความบน Alert Message Box

iOS Message Box Alert iPhone iPad

และแสดงบน Label หลังจากปิด Alert Message Box ไปแล้ว


.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-14 18:21:05 / 2017-03-25 23:03:27
  Download : Download  การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad)
 Sponsored Links / Related

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

 
iOS/iPhone Textbox in Popup - UITextField / UIAlertView (Objective-C,iPhone)
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 01
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 อัตราราคา คลิกที่นี่