Multiple PickerViews in one View?

前端 未结 2 1324
温柔的废话
温柔的废话 2020-12-30 00:50

I want to create 2 separate pickers in the same view using the same viewController. But how do I set separate delegates and datasource for them?

Can\'t seem to get i

2条回答
  •  情歌与酒
    2020-12-30 01:18

    The most straight forward way to do this is to use the tag property of the pickerView. I usually define these in the header for readability. You can set the tag in Interface Builder or in code.

    #define kPickerOne 0
    #define kPickerTwo 1
    

    Then in your implementation file...

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
    {
         if(pickerView.tag == kPickerOne)
         {
              // do something with picker one
         }
         else if(pickerView.tag == kPickerTwo)
         {
              // the other picker
         }
    }
    

提交回复
热议问题