自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何創(chuàng)建一個動態(tài)的Action Sheet

移動開發(fā) iOS
在開發(fā)應(yīng)用App時,SDK絕對算得上是我們的好幫手。而在iOS應(yīng)用中,Action Sheet絕對是必不可少的。本文就將通過Xcode,為大家介紹如何創(chuàng)建一個動態(tài)的Action Sheet。

Step 1: 創(chuàng)建新項(xiàng)目
  打開Xcode,選擇“Create a new Xcode project”,選擇“Single View Application”,點(diǎn)擊“next”。輸入項(xiàng)目名稱(我這么命名為“Fruits”),并確定Devices中選擇的是iPhone,然后勾選上“Use Storyboards”和“Use Automatic Reference Counting”,并點(diǎn)擊“Next”,選擇存放項(xiàng)目的地方,再點(diǎn)擊“create”。

 

Step 2: 設(shè)定支持的方向
我只想讓程序支持縱向模式,所以到 “Supported Interface Orientations”中,取消橫向模式的選中。

 

Step 3: 創(chuàng)建界面
打開工程的Storyboard文件,從 Object Library中拖一個label到View Controller,將這個lable放在View Controller的頂端,并居中,且寬度設(shè)置為280像素。打開Attributes Inspector(屬性面板),將對其方式設(shè)置為居中,最后,刪除lable中的默認(rèn)文本。

  接著,從Object Library拖一個按鈕到View Controller中,將這個按鈕放在label下邊,雙擊按鈕的標(biāo)題,并將標(biāo)題改為“Fruits”。

 


Step 4:連接IBOutlet

  打開ViewController.m,按照如下代碼代碼進(jìn)行修改:
1.#import "ViewController.h"
2.@interface ViewController () <UIActionSheetDelegate>
3.@property(nonatomic, weak)   IBOutlet UILabel *fruitLabel;
4.@property(nonatomic, strong) NSMutableArray *fruits;
5.- (IBAction)showFruits:(id)sender;
6.@end

在上面的代碼中,為label創(chuàng)建了一個插槽(outlet),以及一個存儲fruit的可變數(shù)組,還創(chuàng)建一個action用來顯示“action sheet”。我們還添加了UIActionSheetDelegate,所以當(dāng)你從action sheet中選擇一個fruit的時,我們就可以更新label。


注意,上面所有這些內(nèi)容都是在類擴(kuò)展中實(shí)現(xiàn)的——因?yàn)檫@些屬性和方法沒不需要暴露給別的類。

  現(xiàn)在已經(jīng)建立好了outlet和action,現(xiàn)在我們只需要將它們連接到相應(yīng)的控件中就可以了。打開Storyboard,將fruitsLabel outlet與lable進(jìn)行連接,以及把showFruits:action和button進(jìn)行連接。為選擇Touch Up Inside作為按鈕的控件事件。

 

Step 5: 創(chuàng)建水果列表
打開ViewController.m,創(chuàng)建下邊這樣一個初始化方法:

1.-(id) initWithCoder:(NSCoder *)aDecoder{
2.    if (self = [super initWithCoder:aDecoder]) {
3.        self.fruits = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Orange", @"Banana", @"Strawberry", @"Peach",nil];
4.    }
5.    return self;
6.}

  上面的代碼中創(chuàng)建了一個水果數(shù)組,并在這個數(shù)組中存儲了一些水果。
 
Step 6: 顯示列表

  在didReceiveMemoryWarning方法后面添加如下代碼:
- (IBAction)showFruits:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select a fruit"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    for (NSString *fruit in self.fruits) {
        [actionSheet addButtonWithTitle:fruit];
    }
    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
    [actionSheet showInView:self.view];
}


上面的代碼首先創(chuàng)建一個action sheet,在初始化方法中,我們傳遞了一個標(biāo)題和一個delegate,但是我們沒有添加任何按鈕,甚至是一個取消按鈕。如果我們在這里添加一個取消按鈕,然后再添加其它按鈕,那么取消按鈕就會在列表的最上邊,而不是最下面。


接下來,使用一個for in循環(huán)語句來遍歷之前創(chuàng)建的水果數(shù)組,再這個循環(huán)語句中,我們將所有的水果添加為action sheet的按鈕。循環(huán)語句之后,給action sheet添加了一個cancel按鈕——通過給cancelButtonIndex添加一個標(biāo)題為“Cancel”的按鈕。這樣,action sheet就知道取消按鈕應(yīng)該位于列表的底部,最后,我們以正常的方式將action sheet顯示出來。


Step 7: 更新Fruits Label
在showFruits: action下方添加一個action sheetdelegate協(xié)議。

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        self.fruitLabel.text = [self.fruits objectAtIndex:buttonIndex];
    }
}

  當(dāng)點(diǎn)擊action sheet中的按鈕時,就會調(diào)用上的delegate方法,在方法中,首先判斷一下被按下的按鈕是取消按鈕還是fruit的按鈕——通過對選中按鈕的索引值和取消按鈕的索引值進(jìn)行比較來判斷的。如果選中的是fruit相關(guān)按鈕,那么就將label更新為選中的水果。

 

來源:mobile.tutsplus

責(zé)任編輯:佚名 來源: cocoachina
相關(guān)推薦

2013-06-03 15:38:16

iOS開發(fā)iOS SDK動態(tài)Action Sh

2023-07-06 08:11:35

EasyExcelsheet

2020-06-02 10:04:58

IT部門首席信息官CIO

2023-04-04 09:15:10

NAPI 框架鴻蒙

2009-08-10 18:55:50

創(chuàng)建XSD架構(gòu)文件

2010-08-05 15:46:13

Flex行為Flex效果

2009-08-19 04:14:00

線性鏈表

2017-02-10 20:00:17

Linux共享目錄命令

2020-08-24 07:33:20

CSS框架 SASS

2014-05-23 10:37:37

聊天程序PHP聊天程序

2022-05-16 08:17:36

裝飾器模式

2019-01-07 10:25:44

Gonimo嬰兒監(jiān)視開源

2010-10-26 12:44:38

網(wǎng)絡(luò)監(jiān)控

2022-12-28 08:17:36

數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出

2016-03-08 09:52:22

xcode插件開發(fā)

2012-04-19 17:42:46

Titanium布局

2019-08-12 13:45:26

GithubGit開源

2023-03-13 14:02:31

元宇宙

2012-11-15 09:38:46

2020-09-29 07:24:14

Python字典數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號