2012-09-27 29 views
2

在iPhone的iOS中,我想在配置爲像下拉列表框一樣的情況下製作一個具有與android微調控件類似的外觀和行爲的控件。特別是當按下單選按鈕的文本選項模式列表出現時,當其中一個按下時,列表消失,控制更新爲該選項。例如:
Android spinner example用於下拉列表框(Android微調)風格控制的iOS部分屏幕對話框

到目前爲止,我已經看到了使用[自presentViewController ...]用自定義的ViewController一個全屏幕的選項,但我希望有一個部分屏幕(如上圖)的解決方案。有誰知道如何做到這一點,或者可以指出正確的方向。

回答

0

,如果你沒有想使用UIActionSheet,你想讓它可重複使用的,而不是添加UIViews的整個外灘到當前的廈門國際銀行,你可以創建一個自定義的UIView無論你需要什麼界面來填充它,並使用界面生成器來幫助它看起來不錯。

該視圖可能有一個消息處理程序發佈您需要偵聽的響應。

然後就init和視圖加載到你的子視圖,並填充它

然後從自定義視圖處理程序您註冊

所以對於自定義視圖,你會有這樣的事情發佈消息。

@implementation SomeCustomView 
+(SomeCustomView*)viewFromNibNamed:(NSString *)nibName{ 
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL]; 
    NSEnumerator *nibEnumerator = [nibContents objectEnumerator]; 
    SomeCustomView *customView = nil; 
    NSObject* nibItem = nil; 
    while ((nibItem = [nibEnumerator nextObject]) != nil) { 
     if ([nibItem isKindOfClass:[AADropDown class]]) { 
      customView = (SomeCustomView*)nibItem; 
      break; 
     } 
    } 
    return customView; 
} 
-(void)someInitializationWith:(NSArray*)repeatableData andNotificationId:(NSString*)noteId{ 
//set your stuff up for the view here and save the notification id 
} 
... 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
[[NSNotificationCenter defaultCenter] postNotificationName:Your_Notification_Id object:somevalue]; 
} 
@end 

幷包括其他的東西,如在這種情況下,tableview的東西或任何其他邏輯。

然後在視圖控制器,你可以這樣調用它

__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"customViewAction" object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) { 
    //deal with notification here 
    [[NSNotificationCenter defaultCenter] removeObserver: observer]; 
}]; 
SomeCustomView *cv =(SomeCustomView*) [SomeCustomView viewFromNibNamed:@"SomeCustomView"]; 
[cv someInitializationWith:arrayOptions andNotificationId:@"customViewAction"]; 
[self.view addSubview:cv]; 

,並在你的界面生成器,你只需要確保該類視圖設置爲你的類的類型。

然後,只要用戶需要以相同的方式選擇其他內容,您就可以輕鬆地重新使用此代碼。

+0

難道使用得到一個按鈕和一個字符串數組初始化一個自定義的UIViewController文件類似的東西。然後它將裝飾按鈕看起來像一個下拉列表框(組合框)並處理文本顯示。當按下的代碼調用一個彈出方法來處理剩下的事情。代碼包含在下面的答案中。 – MindSpiker

1

原生解決方案將是一個UIActionSheet,它將在iPhone上從底部出現,部分屏幕或在iPad上與Android版本非常相似。

您可以在這裏找到的文檔:UIActionSheet

0

以下是AtomRiot建議的解決方案的變體。

在您的視圖(xib或storyboard)上創建一個按鈕並將其分配給它。如果它出現在編輯器中,請不要擔心。該代碼將使其成爲可實現的圖形。
enter image description here 2X版本enter image description here

然後在您的項目(以下複製)下列文件:
DDLBHelper.h DDLBHelper.m

然後在你的ViewController的。.h文件生成鏈接到該按鈕:

@property (weak, nonatomic) IBOutlet UIButton *ddlbB; 
- (IBAction)ddlbBClick:(id)sender; 

在你的ViewController的.m文件進行以下電話:

@synthesize ddlbB, choiceLabel; 
DDLBHelper *mDDLBH; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSArray *strings = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil]; 
    mDDLBH = [[DDLBHelper alloc] initWithWithViewController:self button:ddlbB stringArray:strings currentValue:1]; 
} 

- (IBAction)ddlbBClick:(id)sender { 
    [mDDLBH popupList]; 
} 
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    [mDDLBH adjustToRotation]; 
} 

作品就像機器人。

以下是文件:
DDLBHelper.h

// DDLBHelper.h 
// Created by MindSpiker on 9/27/12. 

#import <Foundation/Foundation.h> 
@protocol DDLBHelperDelegate <NSObject> 
@required 
- (void) itemSelected: (int)value; 
@end 

@interface DDLBHelper : UIViewController <UITableViewDelegate, UITableViewDataSource>{ 
    id <DDLBHelperDelegate> delegate; 
} 

@property (retain) id delegate; 

// external interface 
- (id) init; 
- (id) initWithWithViewController:(UIViewController *)viewController button:(UIButton *)button stringArray:(NSArray *)values currentValue:(int) currentValue; 
- (void) popupList; 
- (BOOL) isShown; 
- (void) adjustToRotation; 
- (int) getValue; 
- (NSString *)getValueText; 

@end 

DDLBHelper.m

// DDLBHelper.m 
// Created by MindSpiker on 9/27/12. 

#import "DDLBHelper.h" 
#import <QuartzCore/QuartzCore.h> 

@interface DDLBHelper() { 
@private 
    UIViewController *mVC; 
    UIButton *mButton; 
    NSArray *mValues; 
    int mValue; 

    UITableView *mTV; 

    UIView *mBackgroundV; 

} 

@end 

@implementation DDLBHelper 

@synthesize delegate; 

- (id) init { 
    self = [super init]; 
    mVC = nil; 
    mButton = nil; 
    mValues = nil; 
    mValue = -1; 
    return self; 
} 

- (id) initWithWithViewController:(UIViewController *)viewController button:(UIButton *)button stringArray:(NSArray *)values currentValue:(int) currentValue { 
    self = [super init]; 

    // save pointers 
    mVC = viewController; 
    mButton = button; 
    mValues = values; 
    mValue = currentValue; 

    [self setupButton]; 

    return self; 
} 

- (void) popupList{ 
    if (mBackgroundV == nil){ 
     mBackgroundV = [self setupBackgroundView]; 
     [mVC.view addSubview:mBackgroundV]; 
    } 
    if (mTV == nil){ 
     mTV = [self setupTableView]; 
     [mVC.view addSubview:mTV]; 
    } 
    [mTV reloadData]; 
    [mBackgroundV setHidden:NO]; 
    [mTV setHidden:NO]; 
} 

- (BOOL) isShown{ 
    return !mTV.isHidden; 
} 

- (void) adjustToRotation{ 
    BOOL isShown = [self isShown]; 

    // remove the controls 
    if (mBackgroundV != nil){ 
     [mBackgroundV removeFromSuperview]; 
     mBackgroundV = nil; 
    } 
    if (mTV != nil){ 
     [mTV removeFromSuperview]; 
     mTV = nil; 
    } 

    if (isShown){ 
     [self popupList]; 
    } 
} 

- (int) getValue{ 
    return mValue; 
} 

- (NSString *) getValueText{ 
    if (mValues != nil && mValue > -1) { 
     if (mValues.count > mValue){ 
      return [mValues objectAtIndex:mValue]; 
     } 
    } 
    return nil; 
} 

- (void) updateButtonTitle{ 
    NSString *title = [NSString stringWithFormat:@" %@", [self getValueText]]; 

    [mButton setTitle:title forState:UIControlStateNormal]; 
} 

- (void) setupButton { 
    UIImage *buttonBG = [UIImage imageNamed:@"sis_proceeds_ddlb.png"]; 
    UIEdgeInsets insets = UIEdgeInsetsMake(8, 8, 8, 45); 
    UIImage *sizableImg = [buttonBG resizableImageWithCapInsets:insets]; 
    [mButton setBackgroundImage:sizableImg forState:UIControlStateNormal]; 
    [mButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
    [self updateButtonTitle]; 
} 

- (UIView *) setupBackgroundView{ 
    UIView *v = [[UIView alloc] initWithFrame:mVC.view.bounds]; 
    [[v layer] setOpaque:NO]; 
    [[v layer] setOpacity:0.7f]; 
    [[v layer] setBackgroundColor:[UIColor blackColor].CGColor]; 
    return v; 
} 

- (UITableView *) setupTableView { 
    CGRect rect = [self makeTableViewRect]; 
    UITableView *tv = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain]; 
    [tv setDelegate:self]; 
    [tv setDataSource:self]; 
    [tv setBackgroundColor:[UIColor whiteColor]]; 
    [[tv layer] setBorderWidth:2]; 
    [[tv layer] setBorderColor:[UIColor lightGrayColor].CGColor]; 
    [[tv layer] setCornerRadius:10]; 
    [mVC.view addSubview:tv]; 
    return tv; 
} 

- (CGRect) makeTableViewRect { 
    float l=0.0, t=0.0, w=0.0, h=0.0, maxH=0.0, cellH=0.0, cellsH=0.0; 

    // get 
    l = mButton.frame.origin.x; 
    w = mButton.frame.size.width; 
    t = mVC.view.bounds.origin.y + 50; 
    maxH = mVC.view.bounds.size.height - 100; 

    // get cell height 
    UITableViewCell *c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
    cellH = c.bounds.size.height; 

    // see if list will overlow maxH(eight) 
    cellsH = cellH * mValues.count; 
    if (cellsH > maxH) { 
     h = maxH; 
    } else { 
     h = cellsH; 
    } 

    return CGRectMake(l, t, w, h); 
} 

#pragma mark - TableView Delegate functions 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; // this is a one section table 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return mValues.count; // should be called for only one section 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    // try to resuse a cell if possible 
    static NSString *RESUSE_IDENTIFIER = @"myResuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RESUSE_IDENTIFIER]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RESUSE_IDENTIFIER]; 
    } 

    cell.textLabel.text = [mValues objectAtIndex:indexPath.row]; 
    if (mValue == indexPath.row){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 

} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    // save value and hide view 
    mValue = indexPath.row; 
    [self updateButtonTitle]; 
    [mBackgroundV setHidden:YES]; 
    [mTV setHidden:YES]; 
    [delegate itemSelected:mValue]; 
} 
@end