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,025

HOME > Mobile > [iOS/iPhone] Tutorials - สอนเขียน iPhone App ฟรี เขียน iPad App เรียน iPhone เขียนโปรแกรม iPhone > Objective-C กับการสร้าง Event และ Action (IBAction) โต้ตอบ บน iOS (iPhone,iPad)



Clound SSD Virtual Server

Objective-C กับการสร้าง Event และ Action (IBAction) โต้ตอบ บน iOS (iPhone,iPad)

Objective-C กับการสร้าง Event และ Action (IBAction) โต้ตอบ บน iOS (iPhone,iPad) บทความก่อนหน้านี้เราได้เรียนรู้การเขียน Objective-C เบื้องต้นกับการใช้งาน IBOutlet เชื่อมกับ Object และการสั่งให้ Object ทำงานเพื่อแสดงผลง่ายๆ บนหน้าจอแล้ว บทความนี้เราจะมารู้วิธีการสร้าง Event Action ด้วย IBAction ด้วยการสร้าง App ให้โต้ตอบกับผู้ใช้ โดยมีช่องสำหรับกรอกข้อมูล และปุ่ม Button สำหรับกดหรือ Click และหลังจากคลิกก็ให้แสดงข้อความโต้ตอบกับ User แบบง่าย ๆ บน Label

iOS Objective-C Event Action iPhone iPad

iOS IBAction Sample for iPhone and iPad


ในการสร้าง Action และ Event บน Objective-C จะใช้ IBAction เข้ามาจัดการและสร้าง Event โดยรูปแบบการทำงานของ IBAction จะทำงานเหมือนกับ IBOutlet คือ จะต้องทำการเชื่อมระหว่าง IBAction ที่ประกาศไว้ใน ViewController.h กับ Object ใน ViewController.xib ที่เป็น Interface Builder (ในที่นี่เป็นปุ่ม Button) จากนั้นจะสั่งให้โปรแกรมทำงานได้ใน ViewController.m

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


เริ่มต้นด้วยการสร้าง Project บน Xcode แบบ iOS และ iPhone

iOS Objective-C Event Action iPhone iPad

จะเห็นว่าครั้งแรกสุดหน้าจอของ UI ยังไม่ได้ถูกสร้างด้วย Interface Builder (IB) ยังเป็น Design ที่ว่างเปล่า

iOS Objective-C Event Action iPhone iPad

ทำการลาก Label มาไว้บนหน้าจอ Design ดังรูป

iOS Objective-C Event Action iPhone iPad

ในกรณีที่ต้องการแก้ไขข้อความใน Label สามารถดับเบิ้ลคลิกได้ที่ Label ทันที และจะสามารถแก้ไขข้อความได้

iOS Objective-C Event Action iPhone iPad

ในกรณีที่ต้องการปรับแต่งคุณสมบัติอื่น ๆ ของ Label Object ให้คลิกที่ Inspector ดังรูป

iOS Objective-C Event Action iPhone iPad

จากนั้นให้สร้าง Design ดังรูป โดยประกอบด้วย Label, Round Rect Button และ Text Fields โดลใช้การลากไปวางไว้ในหน้าจอ และประตำแหน่งอื่น ๆ ได้ตามต้องการ








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

iOS Objective-C Event Action iPhone iPad

ให้ประกาศ Object ขึ้นมา 3 ตัวดังรูป

@interface ViewController : UIViewController
{
    IBOutlet UITextField *txtName; // for Text Field (Textbox)
    
    IBOutlet UILabel *lblResponse; // for Label
}

- (IBAction)Hello:(id)sender; // for Button Event

จะเห็นว่าประกอบด้วย IBOutlet 2 ตัว สำหรับ Label และ Text Field และ IBAction 1 ตัว สำหรับ Button และสังเกตุว่าเราตั้งชื่อ IBAction ว่า Hello

กลับมาที่ไฟล์ ViewController.xib

iOS Objective-C Event Action iPhone iPad

คลิกที่ File's Owner ดังรูป

iOS Objective-C Event Action iPhone iPad

ทำการเชื่อม Label กับ lblResponse

iOS Objective-C Event Action iPhone iPad

เชื่อม Text Field กับ txtName

iOS Objective-C Event Action iPhone iPad

ทำการเชื่อม Action ที่มีชื่อว่า Hello กับ Button

iOS Objective-C Event Action iPhone iPad

เลือก Event แบบ Touch up Inside

จากข้างตั้นเราได้ทำการเชื่อม Outlet ที่เป็น IBOutlet Interface ของ Object กับ Object ที่ประกาศไว้ใน ViewController.h ทุกตัวแล้ว ขั้นตอนไปคือการเขียนให้โปรแกรมทำงานตาม Action และ Event ที่เรากำหนด โดยสามารถเขียนได้ที่ไฟล์ ViewController.m

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

iOS Objective-C Event Action iPhone iPad

เขียน Action ดังรูป

- (IBAction)Hello:(id)sender
{
    
    //lblResponse.text = txtName.text;
    //[lblResponse setText:[txtName text]];
    
    NSString *hello = @"สวัสดีครับคุณ ";
    NSString *name = txtName.text;
    NSString *message;
    message = [hello stringByAppendingString:name];
    
    lblResponse.text = message;
    
}

เป็นรูปแบบคำสั่งง่าย ๆ ที่เมื่อคลิกที่ปุ่ม Button ซึ่งประกาศเป็นแบบ IBAction เมื่อมีการคลิก ให้อ่านค่าจาก Text Field ที่ชื่อ txtName มาแสดงบน Label ส่วนตัวแปร message เป็นเพียงการเชื่อมค่าตัวแปรแบบง่าย ๆ

ทดสอบการรันโปรแกรม

iOS Objective-C Event Action iPhone iPad

หลังจากทดสอบโปรแกรม

iOS Objective-C Event Action iPhone iPad

ทดสอบการกรอกชื่อ และคลิกที่ปุ่ม Button โปรแกรมจะแสดงข้อความโต้ตอบแบบง่าย ๆ บน Label








Code ทั้งหมดในบทความนี้

ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITextField *txtName; // for Text Field (Textbox)
    
    IBOutlet UILabel *lblResponse; // for Label
}

- (IBAction)Hello:(id)sender; // for Button Event

@end


ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)Hello:(id)sender
{
    
    //lblResponse.text = txtName.text;
    //[lblResponse setText:[txtName text]];
    
    NSString *hello = @"สวัสดีครับคุณ ";
    NSString *name = txtName.text;
    NSString *message;
    message = [hello stringByAppendingString:name];
    
    lblResponse.text = message;
    
}

- (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.
}

@end


เพิ่มเติม
หากคิดว่าวิธีการตามบทความนี้เช่น การประกาศ Object กับเชื่อม Object กับ Outlet ผ่าน IBOut และ IBAction นั้นเป็นเรื่องยากและซับซ้อน ผมมีวิธีที่ง่ายกว่านั้น ที่จะช่วยให้การเขียนโปรแกรมบน iOS ง่ายกว่าที่จะมานั่งประกาศค่าตัวแปร มาอ่านบทความนี้ดู

Objective-C การสร้าง Outlet Action แบบง่าย ๆ ผ่าน Tools ของ Xcode (iPhone,iPad)



.

   
Share


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


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


   


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

 
โครงสร้างของ Xcode IDE และ ไฟล์ที่ได้จากการ Create Project บน Xcode (iOS, iPhone, iPad)
Rating :

 
การใช้งาน iOS Simulator เบื้องต้น ก่อนการเขียนโปรแกรม iOS สำหรับ iPhone และ iPad
Rating :

 
เพิ่ม Keyboard (Thai Keyboard) ภาษาไทยบน iOS Simulator สำหรับ iPhone และ iPad
Rating :

 
Objective-C และ Outlet (IBOutlet) Action(IBAction) แบบง่าย ๆ ผ่าน Xcode (iPhone,iPad)
Rating :

 
รู้จัก IBOutlet และ IBAction กับ Interface Builder (iOS , iPhone , iPad)
Rating :

 
iOS/iPhone Label(UILabel) , Text Field(UITextField) , Round Rect Button(UIButton) Example (iPhone,iPad)
Rating :

 
รู้จัก NSLog กับ Objective-C บน iOS (for iPhone and iPad Dev)
Rating :

 
iOS/iPhone viewDidLoad , didReceiveMemoryWarning , dealloc (Objective-C , iPhone)
Rating :

 
ฟรี!! อบรม iPhone, เขียน iPhone, เรียน iPhone, สอน iPhone App แหล่งความรู้แบบฟรี ๆ
Rating :

 
สุดยอดการใช้ Object Library เครื่องมือ สร้างออกแบหน้าจอ Interface Builder ในรูปแบบต่าง ๆ ของ iOS ทั้งหมด
Rating :

 
iOS/iPhone Text View (UITextView) Display Multiline Text Region (Objective-C, iPhone, iPad)
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 อัตราราคา คลิกที่นี่