zoukankan      html  css  js  c++  java
  • iOS学习笔记(4)——显示单组件选取器

    1. 创建工程

    • 创建新工程,create a new Xcode project
    • 创建single view application
    • 创建名为PickerViewTest的工程

    2. 创建xib文件

    • New File创建View的user interface
    • 命名为PickerView
    • 在interface builder中拖拽一个Picker View和Button
    • 在dock中把xib的File's Owner 修改为ViewController(系统默认生成的视图控制器文件)
    • 将xib文件的View视图与ViewController中的view(继承自UIViewController)关联
    • 选定Picker View 视图,切换到connect inspector,将Picker View的delegate和dataSource关联到File's Owner(ViewController)
    • 切换到辅助编辑模式,建立picker的IBOutlet输出口,和Button的IBAction动作方法

    3. 编写程序代码

    1)AppDelegate.m文件

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 @interface AppDelegate ()
     4 
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 
    10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    11     // Override point for customization after application launch.
    12     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    13     ViewController *pickerController = [[ViewController alloc] initWithNibName:@"PickerView" bundle:nil];
    14     self.window.rootViewController = pickerController;
    15     self.window.backgroundColor = [UIColor whiteColor];
    16     [self.window makeKeyAndVisible];
    17     return YES;
    18 }
    19 
    20 - (void)applicationWillResignActive:(UIApplication *)application {
    21     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    22     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    23 }
    24 
    25 - (void)applicationDidEnterBackground:(UIApplication *)application {
    26     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    27     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    28 }
    29 
    30 - (void)applicationWillEnterForeground:(UIApplication *)application {
    31     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    32 }
    33 
    34 - (void)applicationDidBecomeActive:(UIApplication *)application {
    35     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    36 }
    37 
    38 - (void)applicationWillTerminate:(UIApplication *)application {
    39     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    40 }
    41 
    42 @end

    2)ViewController.h文件

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
    //必须遵循数据源和委托协议,并实现其相关方法
    @property (strong, nonatomic) NSArray *pickerData;
    //picker的数据源
    
    @property (weak, nonatomic) IBOutlet UIPickerView *picker;
    //辅助编辑器关联picker view的输出口时自动生成
    - (IBAction)pickerSelectedValue:(UIButton *)sender;
    //辅助编辑器关联button的动作方法时自动生成
    @end

    3)ViewController.m文件

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.pickerData = @[@"gthr", @"asdf", @"ghrt", @"bre", @"vew", @"adf", @"wrfe", @"vwfe", @"veww3", @"werw", @"dsfs"];
        self.pickerData = [self.pickerData sortedArrayUsingSelector:@selector(compare:)];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        //该方法返回选择器的组件个数
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        //该方法返回组件的行数
        return [self.pickerData count];
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        //该方法返回数据源的值
        return self.pickerData[row];
    }
    
    - (IBAction)pickerSelectedValue:(UIButton *)sender {
        //测试值是否选择正确
        NSInteger row = [self.picker selectedRowInComponent:0];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Picker Data" message:[NSString stringWithFormat:@"Picker Select %@", self.pickerData[row]] delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
        [alert show];
    }
    
    @end

     4. 小结

    • 必须要使picker view的视图控制器遵循UIPickerViewDataSource, UIPickerViewDelegate协议
    • 必须实现

      - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

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

      - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 三个方法

    • - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 返回组件个数

    • - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 返回选择项的个数
    • - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 返回所选定的值
    • 必须将Picker View的视图与视图控制器中的picker属性进行关联,否则不能获得正确的选择项
    • 必须将View视图与ViewController的view属性进行关联,否则无法加载视图
    • 没有删除main.storyboard,根视图控制器默认指向ViewController,不需要修改
    • ViewController视图控制器作为视图的控制器File‘s Owner,也是Picker View的委托和数据源,实现picker view的协议方法,并为picker view提供所需要显示的数据。
  • 相关阅读:
    Saiku相关异常处理(十五)
    Saiku登录源码追踪.(十三)
    Saiku调用WS接口(十四)
    Saiku本地编译运行后Debug调试(十二)
    Windows查看Java内存使用情况
    Saiku免登录嵌入其他系统使用(十一)
    Saiku更改源代码实现默认查询一天的数据(十)
    Saiku关于MDX过滤的使用-默认显示最近一周的数据(九)
    Saiku部分函数解析(八)
    Saiku缓存处理(七)
  • 原文地址:https://www.cnblogs.com/nycoder/p/4368071.html
Copyright © 2011-2022 走看看