Showing posts with label iOS6. Show all posts
Showing posts with label iOS6. Show all posts

Wednesday, October 24, 2012

แก้ปัญหา Auto Rotate on iOS6

สำหรับ iOS6 SDK นั้นมีคำสั่งหลายตัวเริ่ม Deprecated ไปเหมือนกัน อย่างวันนี้จะเขียนเรื่องของ Autorotate บน iOS6 โดยใน iOS รุ่นต่ำกว่านี้เวลาที่เราต้องการสั่งให้เครื่องตั้งหรือนอน เราจะใช้คำสั่ง

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
}

แต่พอเป็น iOS6 คำสั่งนี้ได้ถูกยกเลิกไป แล้วเพิ่ม 2 คำสั่งนี้เข้ามาแทน

- (NSUInteger)supportedInterfaceOrientations
{
}

- (BOOL)shouldAutorotate
{ 
}

ดังนั้นสิ่งที่ผมจัดการกับปัญหานี้คือสร้าง Category ของ UINavigationController ขึ้นมาแล้วใน ViewController เราก็เรียกใช้คำสั่งเหล่านี้ได้เลย เช่นผมมีคลาส ViewController อยู่หนึ่งตัวก็จะเรียกใช้แบบนี้ได้เลย ตัวแรกจะใช้งานสำหรับ iOS 5 ลงไป

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

ส่วน 2 ตัวนี้จะใช้สำหรับ iOS 6 ขึ้นไปครับ

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

สำหรับ Category นี้ผมเขียนไว้แล้วลอง ดาวโหลด ไปใช้งานกันได้เลยครับ :D


Sunday, October 7, 2012

UIRefresh Control

บทเริ่มแรกขอแนะนำเกี่ยวกับความสามารถใหม่บน iOS6 กันดีกว่านะครับ กำลังมันส์มือ ^^ สำหรับใครที่อัพเดท iOS เป็นเวอร์ชัน 6 ไปเป็นที่เรียบร้อยน่าจะเห็น Pull to Refresh ตัวหนึ่งที่อยู่ใน Mail App นะครับ หน้าตามันจะเป็นแบบดังภาพนี้เลย


คงจะอ๋อกันแล้วนะครับ สำหรับ Refresh ตัวนี้ โดยความสามารถ Apple ได้เปิดให้นักพัฒนาสามารถใช้งานได้เหมือนกันแต่ต้องเป็น iOS6 ขึ้นไปนะครับถึงจะใช้ได้
สำหรับ Control ตัวนี้มีชื่อว่า UIRefreshControl หากใครใช้ XCode 4.5 ขึ้นไป ก็ลองเล่นดูกันได้ครับ 

เริ่มต้นผมสร้าง Object ของ UIRefreshControl ขึ้นมาแล้วเพิ่มเข้าไปเป็น Subview ของ UITableView ต้องเป็น UITableView เท่านั้นนะครับ


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(pullRefresh) forControlEvents:UIControlEventValueChanged];

    [self.tableView addSubview:refreshControl];
}


จากนั้นถ้าเร่ิมมีการ Pull ผมจะให้โปรแกรมเข้าไปทำงานในส่วนของ Method -(void) pullRefresh แล้วส่ัง หยุดการ Refresh หากใครที่นำไปประยุคใช้ก็ให้มาทำคำสั่งที่เราต้องการใน Method นี้ได้เลยครับ เช่น สั่งโหลดข้อมูลล่าสุด แล้ว Reload TableView เป็นต้นครับ

- (void) pullRefresh {
    [refreshControl endRefreshing];
}

สำหรับ RefreshControl ก็มีเท่านี้ครับ ง่ายใช่ไหมละ ลองฝึกใช้กันนะครับ เดี๋ยวก็คล่องแล้ว ส่วนใครอยากลองดาวโหลด Source ไปเล่นดูก็ จิ้มได้เลยครับ