用于下拉列表框(android微调器)样式控件的iOS部分屏幕对话框
在iPhone的iOS中,当配置为类似于下拉列表框时,我想制作一个外观和行为与android spinner控件类似的控件。特别是当按下时,会出现带有单选按钮的文本选项模式列表,当按下其中一个按钮时,列表将消失,控件将更新为该选项。示例:用于下拉列表框(android微调器)样式控件的iOS部分屏幕对话框,ios,cocoa-touch,Ios,Cocoa Touch,在iPhone的iOS中,当配置为类似于下拉列表框时,我想制作一个外观和行为与android spinner控件类似的控件。特别是当按下时,会出现带有单选按钮的文本选项模式列表,当按下其中一个按钮时,列表将消失,控件将更新为该选项。示例: 到目前为止,我已经看到了一个使用[self-presentViewController…]和自定义ViewController的全屏选项,但我想要一个部分屏幕(如上图所示)解决方案。有人知道怎么做吗,或者可以指出正确的方向。本机解决方案将是一个UIActio
到目前为止,我已经看到了一个使用[self-presentViewController…]和自定义ViewController的全屏选项,但我想要一个部分屏幕(如上图所示)解决方案。有人知道怎么做吗,或者可以指出正确的方向。本机解决方案将是一个UIActionSheet,它在iPhone上显示在底部,部分显示在屏幕上,或者在iPad上,与android版本非常相似
您可以在此处找到文档:如果您不想使用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
并包括其他内容,比如在本例中的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];
在界面生成器中,您只需确保将视图的类设置为您的类类型
然后,只要用户需要以相同的方式选择其他内容,您就可以轻松地再次重用此代码。这里是AtomRiot建议的解决方案的一个变体 在视图(xib或故事板)上制作一个按钮并将此图形指定给它。如果它在编辑器中显示为伸展状态,请不要担心。代码将使其成为可实现的图形。
2X版本 然后在项目中包括以下文件(复制如下):
DDLBHelper.h 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
然后在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.h
//由Mindspeaker于2012年9月27日创建。
#进口
@协议DDLBHelperDelegate
@必需的
-(void)itemSelected:(int)值;
@结束
@接口DDLBHelper:UIViewController{
id代表;
}
@属性(保留)id委托;
//外部接口
-(id)init;
-(id)initWithWithViewController:(UIViewController*)viewController按钮:(UIButton*)按钮字符串数组:(NSArray*)值currentValue:(int)currentValue;
-(void)popupList;
-(布尔)伊肖恩;
-(无效)调整旋转;
-(int)getValue;
-(NSString*)getValueText;
@结束
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
//DDLBHelper.m
//由Mindspeaker于2012年9月27日创建。
#导入“DDLBHelper.h”
#进口
@接口DDLBHelper(){
@私人的
UIViewController*mVC;
UIButton*mButton;
NSArray*m值;
int值;
UITableView*mTV;
UIView*mBackgroundV;
}
@结束
@实现DDLBHelper
@综合代表;
-(id)init{
self=[super init];
mVC=零;
姆布顿=零;
M值=零;
mValue=-1;
回归自我;
}
-(id)initWithWithViewController:(UIViewController*)viewController按钮:(UIButton*)按钮字符串数组:(NSArray*)值currentValue:(int)currentValue{
self=[super init];
//保存指针
mVC=视图控制器;
mButton=按钮;
M值=值;
M值=当前值;
[自我设置按钮];
回归自我;
}
-(void)popupList{
如果(mBackgroundV==nil){
mBackgroundV=[自设置背景视图];
[mVC.view addSubview:mBackgroundV];
}
如果(mTV==nil){
mTV=[自设置表视图];
[mVC.view addSubview:mTV];
}
[mTV重新加载数据];
[mBackgroundV setHidden:否];
[mTV设置隐藏:否];
}
-伊肖恩{
返回!mTV.ishiden;
}
-(无效)调整旋转{
BOOL-isShown=[self-isShown];
//卸下控制装置
如果(mBackgroundV!=nil){
[mBackgroundV从SuperView移除];
mBackgroundV=零;
}
如果(mTV!=零){
[mTV从SuperView移除];
mTV=零;
}
如果(isShown){
[自我宣传];
}
}
-(int)getValue{
返回mValue;
}
-(NSString*)getValueText{
if(mValues!=nil&&mValue>-1){
if(mValues.count>mValue){
返回[mValues objectAtIndex:mValue];
}
}
返回零;
}
-(无效)UpdateButtontTitle{
NSString*title=[NSString stringWithFormat:@“%@,[self-getValueText]];
[mButton setTitle:状态的标题:UIControlStateNormal];
}
-(无效)设置按钮{
UIImage*buttonBG=[UIImage ImageName:@“sis_progressions_ddlb.png”];
UIEdgeInsets insets=UIEdgeInsetsMake(8,8,8,45);
UIImage*sizableImg=[buttonBG ResizebleImageWithCapInsets:insets];
[mButton setBackgroundImage:sizableImg for状态:UIControlStateNormal];
[mButton设置内容水平对齐:uicontrol内容水平对齐左];
[自我更新按钮标题];
}
-(UIView*)设置背景视图{
UIView*v=[[UIView alloc]initWithFrame:mVC.view.bounds];
[[v层]设置不透明:否];
[[v层]设置不透明度:0.7f];
[[v layer]setBackgroundColor:[UIColor blackColor].CGColor];
返回v;
}
-(UITableView*)设置表格视图{
CGRect rect=[self-makeTableViewRect];
UITableView*tv=[[UITableView alloc]initWithFrame:rect样式:UITableView样式普通];
[电视设置代表:自我];
[电视设置数据源:self];
[tv setBackgroundColor:[UIColor whiteColor];
[[tv层]宽度:2];
[[tv层]设置顺序颜色:[UIColor lightGra]