简体   繁体   中英

How to declare uisearchController globally in Swift

I'm targeting my app to run in iOS7 and above. I want to put a searchbar in the tableView header. I use UISearchController for iOS8 and above where I put the declaration globally.

var searchController : UISearchController!

However, if I change the target to iOS7, an error shows

'UISearchController' is only available on iOS 8.0 or newer

The XCode suggest that I put

@available(iOS 8.0, *)

attribute to enclosing class in order for me to fix it. By doing this, the whole class only can be run using iOS8 and above.

My goal is to do search using UISearchDisplayController for iOS7 and UISearchController for iOS8 and above. In order to do that, I need to declare it as a global variable as I need the variable to change some behavior of the tableView.

May I know how can I declare the variable globally so I can achieve my goal? Thank you very much.

Before declaring searchController variable, check for iOS version and do your initialisation accordingly. Do something like this

  if #available(iOS 8.0, *) {
                // your iOS 8 related code
            } else {
                // Fallback on earlier versions
            };

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