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 Switch (UISwitch) Example (iPhone,iPad)

iOS/iPhone Switch (UISwitch) Example (iPhone,iPad) สำหรับ Switch Object เป็นรูปแบบ Interface ที่มีลักษณะ เปิด-ปิด หรือ On / Off เป็นค่า Boolean คือ True / False นิยมใช้ในการสร้าง Interface ใน Application เช่น ใน Settings ของ iPhone จะมี Airplane Mode เป็น On หรือ Off และในส่วนของ Setting อื่น ๆ ก็ได้ถูกใช้อีกมากมาย โดย Switch จะทำงานร่วมกับ Class ของ UISwitch ในภาษา Objective-C

iOS/iPhone Switch (UISwitch) Example

iOS/iPhone Switch (UISwitch) Example


การทำงานของ Switch เป็นในรูปแบบง่าย ๆ สามารถสร้างได้จาก Interface Builder ได้ในทันที และสามารถทำการเชื่อม IBOutlet และ IBAction เพื่อทำการ Get ค่าหรือ Set ค่าต่าง ๆ ได้ง่าย ๆ เช่นเดียวกัน เรามาดูรูปแบบคำสั่งแบบง่าย ๆ ของ Switch (UISwitch)

การ Get ค่าของ Switch (UISwitch)

IBOutlet UISwitch *switcher;

if(switcher.isOn)
{
	lblResult.text = @"Switch is On";
}
else
{
	lblResult.text = @"Switch is Off";
}

ในการ Get ค่าสามารถใช้ Property ที่มีชื่อว่า isOn โดยจะ Return ค่ากลับมาเป็น True(1) / False(0) หรือ YES / NO (ซึ่งมีค่าเป็น Boolean เช่นเดียวกัน)

การ Set ค่าของ Switch (UISwitch)

IBOutlet UISwitch *switcher;

[switcher setOn:YES];

[switcher setOn:NO];


Switch (UISwitch)

Switch บน Settings -> Airplane Mode ของ iPhone









Example ตัวอย่างการใช้ Switch ร่วมกับ Class ของ UISwitch แบบง่าย ๆ

iOS/iPhone Switch (UISwitch) Example

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

iOS/iPhone Switch (UISwitch) Example

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

iOS/iPhone Switch (UISwitch) Example

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

iOS/iPhone Switch (UISwitch) Example

ลากรายการของ Switch Object ไมาวางไว้ยังตำแหน่งของหน้าจอ Interface

iOS/iPhone Switch (UISwitch) Example

เราจะสร้าง Label เพื่อแสดงสถานะของ Switch

iOS/iPhone Switch (UISwitch) Example

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

iOS/iPhone Switch (UISwitch) Example

ใน Class ของ .m เราจะได้ Method Event ดังรูป

iOS/iPhone Switch (UISwitch) Example








ใส่คำสั่งแสดงผลแบบง่าย ๆ

- (IBAction)switchChanged:(id)sender {
    
    UISwitch *switcher = (UISwitch *)sender;
    if(switcher.isOn)
    {
        lblResult.text = @"Switch is On";
    }
    else
    {
        lblResult.text = @"Switch is Off";
    }
    
}


Screenshot

iOS/iPhone Switch (UISwitch) Example

ทดสอบบน Simulator แสดงสถานะ On

iOS/iPhone Switch (UISwitch) Example

ทดสอบบน Simulator แสดงสถานะ Off


การ Set ค่าให้กับ Switch ทำงานร่วมกับ UISwitch

iOS/iPhone Switch (UISwitch) Example

สร้างปุ่ม Button ขึ้นมา 2 ตัวดังรูป

iOS/iPhone Switch (UISwitch) Example

เชื่อม IBOutlet และ IBAction ของ Switch และ Button

iOS/iPhone Switch (UISwitch) Example

ตอนนี้ Method Event ของ Button ถูกสร้างขึ้นอัตโนมัติ

iOS/iPhone Switch (UISwitch) Example

ใส่คำสั่งเพื่อ Set ค่าง่าย ๆ

- (IBAction)setToOn:(id)sender {
    
    [switchItem setOn:YES];
     lblResult.text = @"Switch is On";
    
}

- (IBAction)setToOff:(id)sender {
    
    [switchItem setOn:NO];
    lblResult.text = @"Switch is Off";
    
}


Screenshot

iOS/iPhone Switch (UISwitch) Example

คลิก Button (Set to On) เพื่อ Set ค่าเป็น On

iOS/iPhone Switch (UISwitch) Example

คลิก Button (Set to Off) เพื่อ Set ค่าเป็น Off


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

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

#import <UIKit/UIKit.h>

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

- (IBAction)switchChanged:(id)sender;


- (IBAction)setToOn:(id)sender;
- (IBAction)setToOff:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  switchApp
//
//  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.
}

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

- (IBAction)switchChanged:(id)sender {
    
    UISwitch *switcher = (UISwitch *)sender;
    if(switcher.isOn)
    {
        lblResult.text = @"Switch is On";
    }
    else
    {
        lblResult.text = @"Switch is Off";
    }
    
}

- (IBAction)setToOn:(id)sender {
    
    [switchItem setOn:YES];
     lblResult.text = @"Switch is On";
    
}

- (IBAction)setToOff:(id)sender {
    
    [switchItem setOn:NO];
    lblResult.text = @"Switch is Off";
    
}

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

@end



.

   
Share


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


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


   


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

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

 
iOS/iPhone Segmented Control (UISegmentedControl) 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 05
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 อัตราราคา คลิกที่นี่