I wrote a simple function that displays an alert when it is called. I'd like to use this function in several viewControllers. Right now I have the same bit of code copy-pasted into the bottom of each viewController, but I can't help but think there's a better way.
我编写了一个简单的函数,在调用时显示一个警告。我想在几个视图控制器中使用这个函数。现在我有同样的代码复制粘贴到每个视图控制器的底部,但是我不得不认为有更好的方法。
How can one define a function that can be called from any viewController?
如何定义一个可以从任何视图控制器调用的函数?
Just for reference I'll paste my function below, but this is a general question. I'd like to be able to find an eloquent way to handle keyboard management identically across all view controllers as well.
作为参考,我将在下面粘贴我的函数,但这是一个一般的问题。我希望能够找到一种有效的方法来处理所有视图控制器的键盘管理。
func displayAlert(title:String, error:String, buttonText: String) {
// Create the alert
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
// Add an action
alert.addAction(UIAlertAction(title: buttonText, style: .Default, handler: { action in
// Dismiss when the button is pressed
self.dismissViewControllerAnimated(true, completion: nil)
}))
// Add it to viewController
self.presentViewController(alert, animated: true, completion: nil)
}
2 个解决方案
#1
59
Unless you declare the function as private
or fileprivate
, which limit the visibility to the file or scope where it is defined, you can use it anywhere in the module if declared as internal
(the default), and also from external modules if declared as public
or open
.
除非将函数声明为private或fileprivate(这限制了对定义它的文件或范围的可见性),否则如果声明为内部(默认),可以在模块中的任何地方使用它,如果声明为public或open,也可以从外部模块中使用它。
However since you say that you need it in view controllers only, why not implementing it as an extension to the view controller?
但是既然您说您只需要在视图控制器中使用它,为什么不将它作为视图控制器的扩展来实现呢?
extension UIViewController {
func displayAlert(title:String, error:String, buttonText: String) {
...
}
}
#2
2
By Default you get "ViewController.swift" file in your project. you can put some default global function's in it Ex. "Check Internet Connection"
默认情况下你会得到"ViewController "在你的项目中的swift文件。你可以将一些默认的全局函数放入其中。“查看网络连接”
import SystemConfiguration
class func connectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return false
}
var flags : SCNetworkReachabilityFlags = []
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
}
And Call them by
并叫他们
let testConn = ViewController.connectedToNetwork()
#1
59
Unless you declare the function as private
or fileprivate
, which limit the visibility to the file or scope where it is defined, you can use it anywhere in the module if declared as internal
(the default), and also from external modules if declared as public
or open
.
除非将函数声明为private或fileprivate(这限制了对定义它的文件或范围的可见性),否则如果声明为内部(默认),可以在模块中的任何地方使用它,如果声明为public或open,也可以从外部模块中使用它。
However since you say that you need it in view controllers only, why not implementing it as an extension to the view controller?
但是既然您说您只需要在视图控制器中使用它,为什么不将它作为视图控制器的扩展来实现呢?
extension UIViewController {
func displayAlert(title:String, error:String, buttonText: String) {
...
}
}
#2
2
By Default you get "ViewController.swift" file in your project. you can put some default global function's in it Ex. "Check Internet Connection"
默认情况下你会得到"ViewController "在你的项目中的swift文件。你可以将一些默认的全局函数放入其中。“查看网络连接”
import SystemConfiguration
class func connectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return false
}
var flags : SCNetworkReachabilityFlags = []
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
}
And Call them by
并叫他们
let testConn = ViewController.connectedToNetwork()