简体   繁体   中英

Same viewController on both iOS and tvOS apps, UIPickerViewDelegate not available on tvOS

I am using a ViewController for both iOS and tvOS targets and for iOS I use a UIPickerView.

class FirstViewController: UIViewController,
    UITableViewDataSource, UITableViewDelegate, 
    UITextFieldDelegate,UIWebViewDelegate,
    UIPickerViewDataSource, UIPickerViewDelegate

The UIPickerViewDataSource and UIPickerViewDelegate are not available on tvOS, and I'm only using them on iOS, but I get the error UIPickerViewDelegate is unavailable when I try to build for tvOS.

Is there a way to include UIPickerViewDataSource and UIPickerViewDelegate only for iOS app and exclude them when building for tvOS?

this is one way to do it

class Test : UIViewController, UIPickerViewDelegate{

}
#if os(iOS)
extension Test : UIPickerViewDelegate{
    //delegate code goes here!   
}
#endif

but if you need the delegate code to be inside the class i would recommend to to make a super class without the UIPickerViewDataSource and UIPickerViewDelegate and add it in the sub class

 SuperFirstViewController
           |
           |
           /\
          /  \
         /    \
        /      \
      tvOS    iOS - FirstViewController

or in code

class Test : ViewController{
    //some code..
}
class iOSTest : Test, UIPickerViewDelegate{
    //some code..
}
class tvOSTest : Test{
    //some code..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM