UIPicker实现循环联动效果

简介:

用UIPicker实现循环联动效果:

效果图:

具体步骤:

1.新建一个Empty Project,添加一个rootViewController;

2.DXWAppDelegate.h:

#import <UIKit/UIKit.h>

@class rootViewController;

@interface DXWAppDelegate :UIResponder <UIApplicationDelegate>

@property (strongnonatomicUIWindow *window;

@property(nonatomic,strong)rootViewController * rootVController;

@end


DXWAppDelegate.m:

#import "DXWAppDelegate.h"

#import "rootViewController.h"

@implementation DXWAppDelegate


- (void)dealloc

{

    [_window release];

    [_rootVController release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowallocinitWithFrame:[[UIScreenmainScreenbounds]] autorelease];

    self.rootVController = [[rootViewControllerallocinitWithNibName:@"rootViewController"bundle:nil];

    self.window.rootViewController =self.rootVController;

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}

3. rootViewController.h:

#import <UIKit/UIKit.h>

@interface rootViewController :UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property (retainnonatomicIBOutlet UIPickerView *picker;

@property(retain,nonatomic)NSArray *arrChinese;

@property(retain,nonatomic)NSArray *arrPY;

@end


rootViewController.m:

#import "rootViewController.h"

#define component_0 0

#define other_component 1

#define max 16384

@interfacerootViewController ()


@end


@implementation rootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    NSArray *array = [NSArrayarrayWithObjects:@"上海",@"北京",@"天津",@"四川",@"台湾",@"香港",@"江苏",@"湖北",@"山东",@"浙江",nil];

    self.arrChinese = array;

    array = [NSArrayarrayWithObjects:@"shanghai",@"beijing",@"tianjin",@"sichuan",@"taiwai",@"xianggang",@"jiangsu",@"hubei",@"shandong",@"zhejiang",nil];

    self.arrPY = array;

    //用来设置默认选项

    [self.pickerselectRow:max/2inComponent:component_0animated:YES];

    [self.pickerselectRow:max/2inComponent:other_componentanimated:YES];

}


//每个组件有几行数据

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    //创建数据

    if(component ==component_0)

    {

        //return [self.arrChinese count];   //动态获取数字

        return max;

    }else{

        //return [self.arrPY count];

        return max;

    }

}


#pragma mark delegate

//每个组件的每行显示什么数据

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    if(component ==component_0)

    {

        return [self.arrChineseobjectAtIndex:row%([self.arrChinesecount])];

    }else{

        return [self.arrPYobjectAtIndex:row%[self.arrPYcount]];

    }

}


//设置几个Component

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 2;

}


//当你对一个pickerView进行了一次操作之后都会被调用

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    NSString *str =nil;

    if(component ==component_0)

    {

        NSLog(@"%i",row);

        [self.pickerselectRow:row inComponent:other_componentanimated:YES];

    }

    else

    {

        NSLog(@"%i",row);

//        str = [self.arrPY objectAtIndex:row];

        [self.pickerselectRow:row inComponent:component_0animated:YES];

    }

}


- (void)dealloc {

    [_picker release];

    [_arrChineserelease];

    [_arrPY release];

    [super dealloc];

}

@end

注意:在使用picker之前要右击控件到File's owner,将delegate和datasource绑定到File''s owner中
















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366434,如需转载请自行联系原作者

相关文章
|
5天前
|
云安全 人工智能 自然语言处理
|
9天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
847 25
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
425 4
|
12天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
821 59
Meta SAM3开源:让图像分割,听懂你的话
|
2天前
|
弹性计算 网络协议 Linux
阿里云ECS云服务器详细新手购买流程步骤(图文详解)
新手怎么购买阿里云服务器ECS?今天出一期阿里云服务器ECS自定义购买流程:图文全解析,阿里云服务器ECS购买流程图解,自定义购买ECS的设置选项是最复杂的,以自定义购买云服务器ECS为例,包括付费类型、地域、网络及可用区、实例、镜像、系统盘、数据盘、公网IP、安全组及登录凭证详细设置教程:
170 114
|
5天前
|
机器学习/深度学习 人工智能 数据可视化
1秒生图!6B参数如何“以小博大”生成超真实图像?
Z-Image是6B参数开源图像生成模型,仅需16GB显存即可生成媲美百亿级模型的超真实图像,支持中英双语文本渲染与智能编辑,登顶Hugging Face趋势榜,首日下载破50万。
368 19
|
3天前
|
人工智能 安全 小程序
阿里云无影云电脑是什么?最新收费价格个人版、企业版和商业版无影云电脑收费价格
阿里云无影云电脑是运行在云端的虚拟电脑,分企业版和个人版。企业版适用于办公、设计等场景,4核8G配置低至199元/年;个人版适合游戏、娱乐,黄金款14元/月起。支持多端接入,灵活按需使用。
261 164