【问题标题】:Custom Xcode Picker with Identity Inspector带有身份检查器的自定义 Xcode 选择器
【发布时间】:2014-06-19 02:51:30
【问题描述】:

我正在尝试制作一个只有数字 1-10 的自定义 Xcode UIPicker。是否有任何简单的方法可以使用身份检查器执行此操作,或者如果没有,制作这个简单的 UIPicker 的最佳方法是什么?

提前致谢!

【问题讨论】:

    标签: xcode ios7 uipicker


    【解决方案1】:

    不,您不能仅使用 Inerface Builder 创建选择器。 UIPicker 没有等效于 UITableView 的静态单元格。你需要一个视图控制器,但它并不复杂。视图控制器需要实现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 应用程序。创建一个新的项目选择单窗口应用程序。将 ViewController.m 中的代码替换为上面的代码。编辑故事板并添加选择器。 重要您需要将故事板中的选择器视图连接到代码中的IBOutlet

    未连接

    要连接插座,您应该在助手编辑器模式下编辑情节提要。辅助编辑器将显示代码和 IBOutlet 属性。现在“控制单击情节提要中的选取器视图并拖动到声明 IBOutlet 的代码行。当声明 IBOutlet 属性的行的排水沟中的圆圈为黑色时,建立连接。

    已连接

    辅助编辑器模式下的 Xcode 编辑故事板

    仅供参考:到目前为止,学习 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;
    }
    

    【讨论】:

    • 您好,感谢您的快速回复。我是 Xcode 的新手,那么我将如何以及在哪里实现和添加上述代码?然后一旦我这样做了,我如何将我的选择器链接到代码本身?再次感谢@GayleDDS
    • 嗨@user3743364 请参阅我更新的答案以了解完整的操作步骤。干杯。
    • 非常感谢@GayleDDS
    • 非常感谢您的帮助,但如果我想增加文本的大小,我该怎么做?
    • 你需要实现 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 并且可能还有 - (CGFloat )pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 而不是 pickerView:titleForRow:forComponent:
    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多