zoukankan      html  css  js  c++  java
  • iOS开发技巧

    (Swift)

    import UIKit
    
    class ViewController: UIViewController, UIPickerViewDataSource {
        var picker: UIPickerView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            picker = UIPickerView()
            
            // select the current view controller as the data source of the picker view
            picker.dataSource = self
            picker!.delegate = self
            
            picker.center = view.center
            view.addSubview(picker)
        }
        
        /*
            Implemented some of the methods of the UIPickerViewDataSource protocol
        */
        
        // returns the number of 'columns' to display
        func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
            if pickerView == picker {
                return 1
            }
            return 0
        }
        
        // returns the number of rows in each component
        func pickerView(pickerView: UIPickerView,
            numberOfRowsInComponent component: Int) -> Int {
            if pickerView == picker {
                return 10
            }
            return 0
        }
    }
    
    func pickerView(pickerView: UIPickerView,
        titleForRow row: Int,
        forComponent component: Int) -> String! {
        return "(row + 1)"
    }

    (Objective-C)

    @interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
    
    @property (nonatomic, strong) UIPickerView *myPicker;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.myPicker = [[UIPickerView alloc] init];
        
        // select the current view controller as the data source of the picker view
        self.myPicker.dataSource = self;
        self.myPicker.delegate = self;
        
        self.myPicker.center = self.view.center;
        [self.view addSubview:self.myPicker];
    }
    
    /*
        Implemented some of the methods of the UIPickerViewDataSource protocol
    */
    
    - (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;
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView
        titleForRow:(NSInteger)row
        forComponent:(NSInteger)component {
        if ([pickerView isEqual:self.myPicker]) {
            /* Row is zero-based and we want the first row (with index 0)
            to be rendered as Row 1, so we have to +1 every row index */
            
            return [NSString stringWithFormat:@"Row %ld", (long)row + 1];
        }
        
        return nil;
    }
  • 相关阅读:
    Spring框架的反序列化远程代码执行漏洞分析(转)
    JVM调优总结(转)
    创造与熟练与微创造
    一次 react-router 中遇到的小坑
    mac中强大的快捷键
    数组思维 -- join的一些用法感悟
    undefined null 与 字符串相加
    mobx中的数组需要注意的地方
    更换鼠标垫(鼠标)的心路历程
    Object.defineProperty 中的 writable 和 configurable 和 enumerable 的理解
  • 原文地址:https://www.cnblogs.com/davidgu/p/5773764.html
Copyright © 2011-2022 走看看