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 and Timer (NSTimer, Objective-C)

iOS/iPhone and Timer (NSTimer, Objective-C) ในการเขียน iOS App ในกรณีที่ต้องการสั่งให้โปรแกรมทำงานซ้ำ ๆ เราสามารถใช้ Timer ซึ่งอยู่ใน Class ของ NSTimer ในการที่จะสั่งการทำงานซ้ำ ๆ ตามระยะและทุก ๆ หน่วยวินาทีที่กำหนดขึ้น และใน Objective-C การใช้งาน Timer ก็แสนจะง่ายเพียงเขียน Code ไม่กี่บรรทัด

iOS/iPhone and Timer  (NSTimer, Objective-C)

iOS/iPhone and Timer (NSTimer, Objective-C)


Timer หรือ NSTimer อาจจะจำเป็นในการเขียน App ที่ต้องการให้โปรแกรมทำงานทุก ๆ ครั้งที่กำหนด เช่น ในขณะที่ App กำลังเปิดอยู่นั้น เราอาจจะใช้ Timer ตั้งเวลาทุก ๆ 10 วินาที ในการที่จะ Request ไปยัง Web Server เพื่ออ่านข้อล่าสุดมาแสดงบนหน้า App

NSTimer / Objective-C Syntax
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(setCommand) userInfo:nil repeats:YES];

- (void) setCommand
{

}

การสร้าง Timer โดยกำหนดเวลาให้ทำงานทุก ๆ 1 วินาที และเรียก method ชื่อว่า setCommand

[timer invalidate];

สั่งให้ Timer หยุดการทำงาน

Example การใช้งาน Timer (NSTimer แบบง่าย ๆ)

iOS/iPhone and Timer  (NStimer, Objective-C)

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

iOS/iPhone and Timer  (NStimer, Objective-C)

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

iOS/iPhone and Timer  (NStimer, Objective-C)

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








iOS/iPhone and Timer  (NStimer, Objective-C)

ออกแบบ Label และ Button ดังรูป

iOS/iPhone and Timer  (NStimer, Objective-C)

ใน Class ของ .h ให้ทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อย จากนั้นเขียน Code ดังนี้

ViewController.h
//
//  ViewController.h
//  timerApp
//
//  Created by Weerachai on 12/8/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{
    IBOutlet UILabel *lblResult;
    
    NSTimer *timer;
}

- (IBAction)btnStart:(id)sender;
- (IBAction)btnStop:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  timerApp
//
//  Created by Weerachai on 12/8/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    int i;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)btnStart:(id)sender {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(setCommand) userInfo:nil repeats:YES];
}

- (void) setCommand
{
    i = i + 1;
    
    NSMutableString *result;
	result = [NSString stringWithFormat:@"Number = %d",i];
    
    lblResult.text = result;
}

- (IBAction)btnStop:(id)sender {
    [timer invalidate];
}


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

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

@end









Screenshot

iOS/iPhone and Timer  (NStimer, Objective-C)

แสดงหน้าแรกของ App

iOS/iPhone and Timer  (NStimer, Objective-C)

คลิกที่ Start เพื่อให้ Timer เริ่มทำงาน โดยในตัวอย่างนี้จะแสดงการบวกตัวเลขไปเรื่อย ๆ โดยบวกครั้งล่ะ 1 และแสดงผลออกทางหน้าจอ

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-13 09:20:03 / 2017-03-26 09:00:47
  Download : Download  iOS/iPhone and Timer  (NSTimer, Objective-C)
 Sponsored Links / Related

 
การเขียน Coding ด้วยภาษา Objective-C กับการแสดงผลง่าย ๆ บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Dynamic Create Object Dynamically (Objective-C , iPhone)
Rating :

 
Objective-C and Cocoa กับการเขียนโปรแกรมบน iOS สำหรับ iPhone และ iPad
Rating :

 
Objective-C and Variable รู้จักกับตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C and DataType ชนิดของตัวแปรในภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C and Loop การสร้างลูปและแสดง Loop แบบง่าย ๆ (iOS,iPhone,iPad)
Rating :

 
Objective-C and if , else if , switch Flow Condition Statement (iOS,iPhone,iPad)
Rating :

 
Objective-C and Operator การใช้ Operator บนภาษา Objective-C (iOS,iPhone,iPad)
Rating :

 
Objective-C Property(strong,nonatomic) , Weak , Strong type (iOS, iPhone, iPad)
Rating :

 
Objective-C and Class Object (OOP) การเรียกใช้งาน Class (iOS,iPhone,iPad)
Rating :

 
iOS/iPhone Substring / Split / Replace String (Objective-C)
Rating :

 
iOS/iPhone and Thread (NSThread, Objective-C)
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 00
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 อัตราราคา คลิกที่นี่