2

iOS for iPhoneで、ドロップダウンリストボックスのように動作するように構成されている場合、Androidスピナーコントロールと同様の外観と動作のコントロールを作成したいと思います。具体的には、ラジオボタン付きのテキストオプションのモーダルリストが表示され、そのうちの1つを押すとリストが消え、コントロールがその選択に更新されます。例:
Androidスピナーの例

これまで、カスタムViewControllerで[self presentViewController ...]を使用するフルスクリーンオプションを見てきましたが、部分的な画面(上の図のような)ソリューションが必要です。誰かがこれを行う方法を知っているか、正しい方向を指すことができますか?

4

3 に答える 3

1

これに対するネイティブソリューションは、iPhoneでは下から表示されて部分的な画面になるUIActionSheetであり、iPadではAndroidバージョンと非常によく似ています。

ここでドキュメントを見つけることができます:UIActionSheet

于 2012-09-27T17:24:03.340 に答える
0

UIActionSheetを使用せず、現在のXIBに多数のUIViewを追加するのではなく、再利用可能にしたい場合は、入力する必要のあるインターフェイスを使用してカスタム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

この場合、テーブルビューのものやその他のロジックなど、他のものを含めます。

次に、ビューコントローラで次のように呼び出すことができます

__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];

また、Interface Builderでは、ビューのクラスがクラスタイプに設定されていることを確認する必要があります。

そうすれば、ユーザーが同じ方法で他の何かを選択する必要があるときはいつでも、このコードを簡単に再利用できます。

于 2012-09-27T17:53:33.597 に答える
0

これは、AtomRiotによって提案されたソリューションのバリエーションです。

ビュー(xibまたはストーリーボード)でボタンを作成し、このグラフィックをそれに割り当てます。エディターで引き伸ばされて表示されても心配しないでください。コードはそれを実現可能なグラフィックにします。
ここに画像の説明を入力してください 2Xバージョンここに画像の説明を入力してください

次に、次のファイルをプロジェクトに含めます(以下にコピー):
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];
}

Androidと同じように機能します。

ファイルは次のとおりです:
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
于 2012-09-28T19:13:07.017 に答える