使用Identity Inspector自定义Xcode选取器

时间:2014-06-19 02:51:31

标签: xcode ios7 uipicker

我正在尝试使用仅1-10的自定义Xcode UIPicker。是否有任何简单的方法可以使用Identity Inspector执行此操作,或者如果没有,那么制作这个简单的UIPicker的最佳方法是什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

不,您不能仅使用Inerface Builder创建选取器。对于UITableView,没有相当于UIPicker个静态单元格。你需要一个视图控制器,但它并不复杂。视图控制器需要实现UIPickerViewDataSourceUIPickerViewDelegate个委托。以下是单列选择器的基础知识,值为1到10.

#import "ViewController.h"

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.pickerView.delegate   = self;
    self.pickerView.dataSource = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIPickerView Data Source & Delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%d", row + 1 ];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // TODO do something with the selection
}

@end

使用选择器创建最小的iOS应用程序。创建一个新的Project select单窗口应用程序。用上面的方法替换ViewController.m中的代码。编辑故事板并添加选择器。 重要您需要将故事板中的选择器视图连接到代码中的IBOutlet

未连接 IBOutlet not Connected

要连接插座,您应该在助理编辑器模式下编辑故事板。辅助编辑器将显示代码和IBOutlet属性。现在“控制”单击故事板中的选择器视图并拖动到声明IBOutlet的代码行。当声明IBOutlet属性的行上的装订线中的圆圈为黑色时,将建立连接。

<强>联网 IBOutlet Connected

以助理编辑器模式编辑故事板 Xcode Editing Storyboard in Assistant Editor Mode

仅供参考:到目前为止,学习iOS的最佳和最快速的方式是斯坦福iTunes U课程:

为iPhone和iPad开发iOS 7应用程序 https://itunes.apple.com/us/course/developing-ios-7-apps-for/id733644550

您需要观看一些演示,以了解如何建立连接Interface Builder(IB)。您需要了解的第一件事是IB不会创建资源或配置文件。故事板文件包含序列化的界面对象。几乎与您可以在代码中创建的对象相同。

更改选择器文本的大小

您需要将pickerView:titleForRow:forComponent:的实施替换为您自己的pickerView:rowHeightForComponent:pickerView:viewForRow:forComponent:reusingView:

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 60;  // Row height in points, Note this should match the height of the UILabel Rect.
}

// FYI points are not pixels on non retina screens 1 point = 1 pixel but on retina screens 1 point = 2 pixels

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (view) {
        UILabel *reuseLabel = (UILabel *)view;
        reuseLabel.text = [NSString stringWithFormat:@"%ld", row + 1 ];
        return reuseLabel;
    }

    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
    newLabel.text = [NSString stringWithFormat:@"%ld", row + 1 ];
    newLabel.font = [UIFont systemFontOfSize:42];
    newLabel.textAlignment = NSTextAlignmentCenter;

    return newLabel;
}
相关问题