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 Adding Show Image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

iOS/iPhone Adding Show Image Action Sheet (UIActionSheet) and Alert View (UIAlertView) บทความ iOS นี้จะเป็นการใส่ Image หรือ Icons ลงใน Action Sheet (UIActionSheet) และ Alert View (UIAlertView) โดยรูปภาพเหล่านี้จะไปแสดงด้านหน้าของแต่ล่ะ item นั้น ๆ และสำหรับรูปภาพควรเป็นภาพที่มีนามสกุล png แบบโปร่งแสง และขนาดให้พอดีกับตำแหน่งที่ต้องการ ส่วนวิธีการใส้ Image Icons ลงใน Action Sheet และ Alert View นั้น ทำได้ง่าย ๆ ลองมาดูตังอย่าง

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)


ในการใส่ Image Icons นั้น จะใช้การสร้าง Action Sheet และ Alert View ตามปกติ เพียงแต่เพิ่มคุณสมบัติ setImage ของแต่ล่ะ Item โดยการอ้างถึงแต่ล่ะ Index ใน Action Sheet และ Alert View นั้น ๆ ก็สามารถที่จะสร้างได้ในทันที

    sheet = [[UIActionSheet alloc] initWithTitle:@"Shared To"
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                          destructiveButtonTitle:@"Facebook"
                               otherButtonTitles:@"Twitter", @"Google Plus", @"Youtube", nil];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"facebook.png"] forState:UIControlStateNormal];
   
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"twitter.png"] forState:UIControlStateNormal];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"google-plus.png"] forState:UIControlStateNormal];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"youtube.png"] forState:UIControlStateNormal];
    
    // Show the sheet
    [sheet showInView:self.view];
    [sheet release];


Example ตัวอย่างการสร้าง Image Icons บน Action Sheet (UIActionSheet) and Alert View (UIAlertView)

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

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

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

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

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

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

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

อันนี้เป็น Image แบบนามสกุล png โปรงใส่ ให้ Copy ลงในโฟเดอร์ของ Project และคลิกขวาที่ Project เลือก Add Files to... และเลือกไฟล์ต่าง ๆ เหล่านั้น








iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

ไฟล์รูปภาพเข้ามาใน Project เรียบร้อยแล้ว

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

ออกแบบหน้าจอ View ด้วย Label และ Button ดังรูป

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

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

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIActionSheetDelegate>
{
    IBOutlet UILabel *lblResult;
}

- (IBAction)btnOpenSheet:(id)sender;

@end


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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UIActionSheet *sheet;
}

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

- (IBAction)btnOpenSheet:(id)sender {
    
    sheet = [[UIActionSheet alloc] initWithTitle:@"Shared To"
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                          destructiveButtonTitle:@"Facebook"
                               otherButtonTitles:@"Twitter", @"Google Plus", @"Youtube", nil];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"facebook.png"] forState:UIControlStateNormal];
   
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"twitter.png"] forState:UIControlStateNormal];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"google-plus.png"] forState:UIControlStateNormal];
    
    [[[sheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"youtube.png"] forState:UIControlStateNormal];
    
    // Show the sheet
    [sheet showInView:self.view];
    [sheet release];
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            lblResult.text = @" Your Selected Item 0";
            break;
        case 1:
            lblResult.text = @" Your Selected Item 1";
            break;
        case 2:
            lblResult.text = @" Your Selected Item 2";
            break;
        case 3:
            lblResult.text = @" Your Selected Item 3";
            break;
        case 4:
            lblResult.text = @" Your Selected Item 4";
            break;
        default:
            break;
    }
}

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

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

@end


Screenshot

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

แสดงหน้าจอ View บน iOS iPhone Simulator

iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

เมื่อแสดง Action Sheet (UIActionSheet) ตอนนี้จะมี Icons แสดงในแต่ล่ะ item แล้ว








เพิ่มเติม สำหรับ Alert View (UIAlertView)

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Shared!"
                          message:@"Share to ?"
                          delegate: self
                          cancelButtonTitle:@"Facebook"
                          otherButtonTitles:@"Twitter", nil];
    
        [[[alert valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"facebook.png"] forState:UIControlStateNormal];
    
        [[[alert valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"twitter.png"] forState:UIControlStateNormal];
    
    [alert show];
    [alert release];


iOS/iPhone Adding image Action Sheet (UIActionSheet) and Alert View (UIAlertView)

แสดง Image icons บน Popup ของ Apert View (UIAlertView)

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-06 09:30:58 / 2017-03-26 00:21:11
  Download : Download  iOS/iPhone Adding Show Image Action Sheet (UIActionSheet) and Alert View (UIAlertView)
 Sponsored Links / Related

 
การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Confirm Dialog Popup - UIAlertView (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Textbox in Popup - UITextField / UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Dialog Popup and Optional Item - UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Username and Password Popup - UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Action Sheet Popup - UIActionSheet (Objective-C,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 อัตราราคา คลิกที่นี่