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 Segmented Control (UISegmentedControl) Example (iPhone,iPad)

iOS/iPhone Segmented Control (UISegmentedControl) Example (iPhone,iPad) สำหรับ Segmented Control เป็น Object ที่ใช้สำหรับแบ่ง หรือ แยกย่อยของแต่ล่ะส่วน คล้าย ๆ กับการทำงานของ Tab Bar แต่ Segmented จะทำงานในรูปแบบของ View เดียวกัน คือภายใน Segmented 1 Object จะประกอบด้วยหลาย ๆ Segments Item หรือหลายส่วน เมื่อเราคลิกในแต่ล่ะส่วนเราสามารถที่จะทำการอ่านค่า Index ว่าปัจจุบันนั้น Segmented อยู่ใน Index ที่เท่าไหร่และเขียนเงื่อนไขต่าง ๆ ให้โปรแกรมทำงานตามที่ต้องการ โดย Segmented จะทำงานร่วมกับ Class ของ UISegmentedControl บนภาษา Objective-C และการใช้งานก็ง่าย ๆ ไม่ซับซ้อยมากมาย

iOS/iPhone Segmented (UISegmentedControl)

iOS/iPhone Segmented (UISegmentedControl) Example


การ Get ค่าของ Segmented (UISegmentedControl)
IBOutlet UISegmentedControl *segmentedItem;

int indexSelect = segmentedItem.selectedSegmentIndex;


การ Set ค่าของ Segmented (UISegmentedControl)
IBOutlet UISegmentedControl *segmentedItem;

segmentedItem.selectedSegmentIndex = 2;

โดย Index ของ Segmented Control จะเริ่มจาก 0

Segmented Control เป็น Object ที่ถูกใช้งานกับหลาย ๆ ส่วนของ Application บน iPhone หรือ iPad เช่นใน iPhone ตรง Recents Call จะมี Segmented ทำงานอยู่ โดยมี Item ของ All และ Missed อยู่ในขณะนั้น และ Application อื่น ๆ ที่เคยพบเจอก็เช่นของ Thairath LITE ที่แบ่ง Segmented ออกเป็น หน้าแรก , การเมือง , กีฬา , บันเทิง เป็นต้น

iOS/iPhone Segmented (UISegmentedControl)

Segmented (UISegmentedControl) กับการใช้งานจริงบน App ของ Thairath LITE


ในบทความนี้เราจะมารู้จักกับ Segmented และการใช้งาน Segmented ร่วมกับ Class ของ UISegmentedControl ในการสร้างและการ Get ค่า หรือ Set ค่า รวมทั้งการปรับแต่ง Segmented ร่วมกับการเขียน Code ในภาษา Objective-C แบบง่าย ๆ เพื่อเป็นพื้นฐานในการนำไปประยุกต์ใช้กับส่วนอื่น ๆ ได้

iOS/iPhone Segmented (UISegmentedControl)

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








iOS/iPhone Segmented (UISegmentedControl)

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

iOS/iPhone Segmented (UISegmentedControl)

ตอนนี้หน้าจอ View ของเราจะยังว่าง ๆ

iOS/iPhone Segmented (UISegmentedControl)

ทำการลาก Segmented Control มาไว้ยังหน้าจอ Interface

iOS/iPhone Segmented (UISegmentedControl)

เราสามารถเพิ่มจำนวนของ Segments ได้จาก Inspector ว่า Segments

iOS/iPhone Segmented (UISegmentedControl)

คลิกเพื่อจะเข้าไปแก้ไขข้อความ String Title ของแต่ล่ะ Segments

iOS/iPhone Segmented (UISegmentedControl)

เลือก Segments Item ที่ต้องการ โดยขึ้นอยู่กับจำนวนที่ได้กำหนดไว้ก่อนหน้านี้

iOS/iPhone Segmented (UISegmentedControl)

ปรับแต่งแก้ไข Title

iOS/iPhone Segmented (UISegmentedControl)

ในตัวอย่างตั้งชื่อ Item 1 ถึง Item 5

iOS/iPhone Segmented (UISegmentedControl)

เราจะเพิ่ม Label ไว้สำหรับแสดงค่าปัจจุบันของตำแหน่งของ Segments ที่ได้คลิกเลือก

iOS/iPhone Segmented (UISegmentedControl)

ใน Class ของ .h ทำการเชื่อม IBOutlet และ IBAction ดังรูป

iOS/iPhone Segmented (UISegmentedControl)

ใน Class ของ .m ได้ Method Event ดังรูป ตอนนี้ยังไม่มีคำสั่งใด ๆ

iOS/iPhone Segmented (UISegmentedControl)

การอ่านตำแหน่ง Index ของ Segmented สามารถใช้คำสั่ง segmented.selectedSegmentIndex โดยอ้างอิงจาก Index ที่เริ่มจาก 0

- (IBAction)segmentedChanged:(id)sender {
    
    UISegmentedControl *segmented = (UISegmentedControl *)sender;

    switch (segmented.selectedSegmentIndex) {
        case 0:
            selItem.text =@"Segment Item 1 selected.";
            break;
        case 1:
            selItem.text =@"Segment Item 2 selected.";
            break;
        case 2:
            selItem.text =@"Segment Item 3 selected.";
            break;
        case 3:
            selItem.text =@"Segment Item 4 selected.";
            break;
        case 4:
            selItem.text =@"Segment Item 5 selected.";
            break;
        default:
            break;
    }


Screenshot

iOS/iPhone Segmented (UISegmentedControl)

ผลลัพธ์ของ Segmented กับตำแหน่งที่คลิกเลือก

iOS/iPhone Segmented (UISegmentedControl)

ผลลัพธ์ของ Segmented กับตำแหน่งที่คลิกเลือก








การ Set ค่า Index ให้กับ Segmented Control

iOS/iPhone Segmented (UISegmentedControl)

ทำการสร้าง IBOutlet ให้กับ Segmented

iOS/iPhone Segmented (UISegmentedControl)

ใน Method ของ viewDidLoad สามารถกำหนดค่า selectedSegmentIndex ได้ทันที โดย Index จะเริ่มจาก 0

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    segmentedItem.selectedSegmentIndex = 2;
    
    selItem.text =@"Segment Item 3 selected.";
    
}


Screenshot

iOS/iPhone Segmented (UISegmentedControl)

เพื่อรันโปรแกรมบน Simulator ก็จะทำการ Select ตำแหน่ง Index ที่เรากำหนดขึ้น

เพื่มเติม

iOS/iPhone Segmented (UISegmentedControl)

ลองเปลี่ยน Style ของ Segmented Control เป็นแบบ Bar

iOS/iPhone Segmented (UISegmentedControl)

ได้ผลลัพธ์ดังรูป

ทั้งนี้สามารถประบแต่งสี หรือคุณสมบัติอื่น ๆ ในส่วนของ Inspector ได้ โดยจะมีคุณสมบัติเช่น ใส่รูปภาพ สีพื้นหลัง สีพื้นของ Selected และอื่น ๆ



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

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *selItem;
    
    IBOutlet UISegmentedControl *segmentedItem;
}

- (IBAction)segmentedChanged:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  segmentedApp
//
//  Created by Weerachai on 11/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.
    
    segmentedItem.selectedSegmentIndex = 2;
    
    selItem.text =@"Segment Item 3 selected.";
    
}

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

- (IBAction)segmentedChanged:(id)sender {
    
    UISegmentedControl *segmented = (UISegmentedControl *)sender;

    switch (segmented.selectedSegmentIndex) {
        case 0:
            selItem.text =@"Segment Item 1 selected.";
            break;
        case 1:
            selItem.text =@"Segment Item 2 selected.";
            break;
        case 2:
            selItem.text =@"Segment Item 3 selected.";
            break;
        case 3:
            selItem.text =@"Segment Item 4 selected.";
            break;
        case 4:
            selItem.text =@"Segment Item 5 selected.";
            break;
        default:
            break;
    }

}

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

@end


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-13 16:43:33 / 2017-03-26 00:15:58
  Download : Download  iOS/iPhone Segmented Control (UISegmentedControl) Example (iPhone,iPad)
 Sponsored Links / Related

 
iOS/iPhone Slider (UISlider) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Switch (UISwitch) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Stepper (UIStepper) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Map View (MKMapView) Longitude , Latitude Example (iPhone,iPad)
Rating :

 
iOS/iPhone Picker View (UIPickerView) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Collection View (UICollectionViewController) Multiple Column Item (iPhone, iPad)
Rating :

 
iOS/iPhone Scroll View (UIScrollView) Example (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 อัตราราคา คลิกที่นี่