用于下拉列表框(android spinner)样式控件的iOS部分屏幕对话框

时间:2012-09-27 17:14:09

标签: ios cocoa-touch

在iPhone for iOS中,我希望在配置为像下拉列表框一样的情况下,为android spinner控件创建一个具有类似外观和行为的控件。特别是当按下带有单选按钮的模式文本选项列表时,当按下其中一个时,列表消失,控件更新为该选项。例如:
Android spinner example

到目前为止,我已经看到使用[self presentViewController ...]和自定义ViewController的全屏选项,但我想要一个部分屏幕(如上图所示)解决方案。有谁知道如何做到这一点或者可以指出正确的方向。

3 个答案:

答案 0 :(得分:1)

本地解决方案将是一个UIActionSheet,在iPhone上将从底部显示,部分屏幕或在iPad上与Android版本非常相似。

您可以在此处找到文档:UIActionSheet

答案 1 :(得分:0)

如果您不想使用UIActionSheet并且想要使其可重用而不是将整个UIViews包添加到当前的XIB中,您可以创建一个自定义UIView,其中包含填充它所需的任何接口并使用该接口建设者,以帮助使它看起来不错。

该视图可以有一个消息处理程序,用于发布您需要监听的响应。

然后只需初始化并将视图加载到子视图中并填充它

然后将自定义视图中的消息发布到您注册的处理程序

所以对于你的自定义视图,你会有这样的东西。

@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内容或任何其他逻辑。

然后在你的viewcontroller中你可以像

一样调用它
__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];

并且在界面构建器中,您只需要确保将视图的类设置为类类型。

然后,只要用户需要以相同的方式选择其他内容,您就可以轻松地重复使用此代码。

答案 2 :(得分:0)

以下是AtomRiot建议的解决方案的变体。

在您的视图(xib或故事板)上创建一个按钮并将此图形指定给它。如果在编辑器中显示出来,请不要担心。代码将使其成为可实现的图形 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