Picking Values with the UIPicker View

 

Problem

You want to allow the users of your app to select from a list of values.

Solution

Use the UIPickerView class.

Discussion

A picker view is a graphical element that allows you to display a series of values to your users and allow them to pick one. The Timer section of the Clock app on the iPhone is a great example of this (Figure 1-10).

First let’s go to the top of the .m (implementation) file of our view controller and define our picker view:

@interface ViewController ()


@property (nonatomic, strong) UIPickerView *myPicker;

@end

@implementation ViewController

...

Now let’s create the picker view in the viewDidLoad method of our view controller:

- (void)viewDidLoad{

      [super viewDidLoad];

      self.myPicker = [[UIPickerView alloc] init];

      self.myPicker.center = self.view.center;

      [self.view addSubview:self.myPicker];

}

It’s worth noting that in this example, we are centering our picker view at the center of our view. When you run this app on iOS 7 Simulator, you will see a blank screen because the picker on iOS 7 is white and so is the view controller’s background.

The reason this picker view is showing up as a plain white color is that we have not yet populated it with any values. Let’s do that. We do that by specifying a data source for the picker view and then making sure that our view controller sticks to the protocol that the data source requires. The data source of an instance of UIPickerView must conform to the UIPickerViewDataSource protocol, so let’s go ahead and make our view controller conform to this protocol in the .m file:

 

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *myPicker;


@end

@implementation ViewController ...

 

Good. Let’s now change our code in the implementation file to make sure we select the current view controller as the data source of the picker view:

 

- (void)viewDidLoad{ [super viewDidLoad];

      self.myPicker = [[UIPickerView alloc] init];

      self.myPicker.dataSource = self;

      self.myPicker.center = self.view.center;

      [self.view addSubview:self.myPicker];

}

After this, if you try to compile your application, you will get warnings from the compiler telling you that you have not yet implemented some of the methods that the UIPicker ViewDataSource protocol wants you to implement. The way to fix this is to press Com‐ mand+Shift+O, type in UIPickerViewDataSource, and press the Enter key on your keyboard. That will send you to the place in your code where this protocol is defined, where you will see something similar to this:

 

@protocol UIPickerViewDataSource<NSObject> @required

    // returns the number of 'columns' to display.

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

    // returns the # of rows in each component..

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


@end

 

Can you see the @required keyword there? That is telling us that whichever class wants to become the data source of a picker view must implement these methods. Good deal. Let’s go implement them in our view controller’s implementation file:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

  if ([pickerView isEqual:self.myPicker]){ return 1;

  }

  return 0;

}

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

{

  if ([pickerView isEqual:self.myPicker])

  {

   return 10;

  }

  return 0;

 }

转载于:https://www.cnblogs.com/ios-mark/p/3759928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
摘 要 伴随着人才教学的关注度越来越高,对于人才的培养也是当今社会发展的最为重要的问题之一。为了进一步的进行人才的培养关工作,许多的学校或者是教育的机构逐步的开展了网络信息化的教学和和管理工作,通过信息化的手段和技术实现网络信息化的教育及管理模式,通过网络信息化的手段实现在线答题在线考试和学生信息在线的管理等操作。这样更加的快捷解决了人才培养之中的问题,也在进一步的促进了网络信息化教学方式的快速的发展工作。相较于之前的人才教育和培养工作之中,存在这许多的问题和局限性。在学生信息管理方面通过线下管理的形式进行学生信息的管理工作,在此过程之中存在着一定的局限性和低效性,往往一些突发的问题导致其中工作出现错误。导致相关的教育工作受到了一定的阻碍。在学生信息和学生成绩的管理方面,往常的教育模式之下都是采用的是人工线下的进行管理和整理工作,在这一过程之中存在这一定的不安全和低效性,面对与学生基数的越来越大,学生的信息管理也在面领着巨大的挑战,管理人员面领着巨大的学生信息的信息量,运用之前的信息管理方式往往会在统计和登记上出现错误的情况的产生,为后续的管理工作造成了一定的困难。然而通过信息化的管理方式进行对学生信息的管理不仅可以避免这些错误情况的产生还可以进一步的简化学生信息管理工作的流程,节约了大量的人力和物力的之处。在线答题系统的实现不仅给学生的信息管理工作和在线考试带来了方便也进一步的促进了教育事业信息化的发展,从而实现高效化的教学工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值