天天看點

UIPicker實作循環關聯效果

用UIPicker實作循環關聯效果:

效果圖:

UIPicker實作循環關聯效果

具體步驟:

1.建立一個Empty Project,添加一個rootViewController;

2.DXWAppDelegate.h:

#import <UIKit/UIKit.h>

@class rootViewController;

@interface DXWAppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,

nonatomic) UIWindow *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 = [[[UIWindowalloc]

initWithFrame:[[UIScreenmainScreen]

bounds]] autorelease];

    self.rootVController = [[rootViewControlleralloc]

initWithNibName:@"rootViewController"bundle:nil];

    self.window.rootViewController =self.rootVController;

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

3.rootViewController.h:

@interface rootViewController :UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

@property (retain,

nonatomic) IBOutlet UIPickerView *picker;

@property(retain,nonatomic)NSArray *arrChinese;

@property(retain,nonatomic)NSArray *arrPY;

rootViewController.m:

#define component_0 0

#define other_component

1

#define max 16384

@interfacerootViewController ()

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

#pragma mark delegate

//每個元件的每行顯示什麼資料

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

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

        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;

        NSLog(@"%i",row);

        [self.pickerselectRow:row

inComponent:other_componentanimated:YES];

    else

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

inComponent:component_0animated:YES];

- (void)dealloc {

    [_picker

    [_arrChineserelease];

    [_arrPY

    [super

dealloc];

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

繼續閱讀