iOS/iPhone Dynamic Create Object Dynamically (Objective-C , iPhone) |
iOS/iPhone Dynamic Create Object Dynamically (Objective-C , iPhone) ปกติแล้วเวลาเราสร้าง Object ต่าง ๆ บนหน้าจอ Interface Design ของ View เราจะใช้การ Drag and Drop รายการ Object ต่าง ๆ ไว้บนหน้าจอ View ได้ทันที และถ้าต้องการปรับแต่งคุณสมบัติอื่น ๆ ของ Object ก็สามารถคลิกได้ที่ Inspector และกำหนดค่าคุณสมบัติต่าง ๆ ตามหัวข้อนั้น ๆ แต่ในบางครั้งเราอาจจะต้องสร้าง Object แบบ Dynamically Runtime หรือสร้างในขณะที่โปรแกรมกำลังทำงานอยู่ ซึ่งเราอจจะต้องได้ใช้บ่อย ๆ ถึงบ่อยมาก ในการเขียน App บน iPhone และ iPad ด้วยภาษา Objective-C
iOS/iPhone Dynamic Create Object Dynamically
ในการสร้าง Object นั้นใช้หลักการง่าย ๆ ก็คือการเรียกใช้ Class ของ Object นั้น ๆ เช่น Label ก็จะสร้าง Class ของ UILabel และกำหนดคุณสมัติอื่น ๆ เช่น ตำแหน่ง , ข้อความ , สี และหลังจากได้ Object เรียบร้อยแล้วก็จะนำลงบน View ด้วยคำสั่ง addSubview
UILabel *txt=[[UILabel alloc] initWithFrame:CGRectMake(135,0,100 ,30)];
txt.text = @"My Label";
txt.textColor = [UIColor blackColor];
[[self view] addSubview:txt];
[txt release];
ตัวอย่างการสร้าง Label แบบ Dynamically Runtime
คำอธิบาย
// ประกาศสร้าง Class แบบ Label ส่วน CGRectMake คือตำแหน่ง
UILabel *txt=[[UILabel alloc] initWithFrame:CGRectMake(135,0,100 ,30)];
txt.text = @"My Label"; // แสดงข้อความ MyLabel
txt.textColor = [UIColor blackColor]; // กำหนดสีดำ
[[self view] addSubview:txt]; // แสดงผลบนหน้าจอ View
[txt release]; // ปล่อย Resource ของ Object ที่ได้สร้างขึ้น เพื่อ Clear Memory
Example การสร้าง Dynamic Create Object บน Dynamically Runtime

สร้าง Application บน Xcode แบบ Single View Application

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

ตอนนี้หน้าจอ View จะยังว่างเปล่า เรามาสร้าง Dynamic Create Object Dynamically บน Code ของ Objective-C ดังนี้

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C
ViewController.h
//
// ViewController.h
// dynamicObject
//
// Created by Weerachai on 11/17/55 BE.
// Copyright (c) 2555 Weerachai. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
//
// ViewController.m
// dynamicObject
//
// Created by Weerachai on 11/17/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.
UILabel *txt=[[UILabel alloc] initWithFrame:CGRectMake(135,0,100 ,30)];
txt.text = @"My Label";
txt.textColor = [UIColor blackColor];
[[self view] addSubview:txt];
[txt release];
for(int i = 0; i < 10; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(120, 50 + i*35, 100, 30)];
NSMutableString *text= [NSString stringWithFormat:@"Click Me %d",i];
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClck) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:button];
[button release];
}
}
-(void) btnClck {
NSLog(@"Button Pressed!");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
สำหรับ Code ของ Objective-C เป็นรุปแบบที่ง่าย ๆ อ่านแล้วเข้าใจได้ในทันที
Screenshot

.
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
  |
|
|
Create/Update Date : |
2012-11-20 09:59:17 /
2017-03-25 22:56:04 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|