xcode 如何创建一个所有视图控制器都可以访问的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27605909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a method that all view controllers have access to?
提问by dom999999
By the time my app is complete, I'll have 50 or so view controllers. In order to use my app the user must have a working internet connection.
到我的应用程序完成时,我将拥有 50 个左右的视图控制器。为了使用我的应用程序,用户必须有可用的互联网连接。
I have a code that checks if there is a valid connection, however I would have to add the same code to every viewDidLoad
or viewDidAppear
for each ViewController (very inefficient in my eyes as I have a Swift file for every VC)
我有一个代码来检查是否存在有效连接,但是我必须为每个viewDidLoad
或viewDidAppear
每个 ViewController添加相同的代码(在我看来效率非常低,因为我为每个 VC 都有一个 Swift 文件)
Is there a way to create a "universal" method where I only have the Internet connection code present in one place (and all ViewControllers are able to access it)?
有没有办法创建一种“通用”方法,在这种方法中我只有一个地方存在 Internet 连接代码(并且所有 ViewController 都可以访问它)?
I'm using Swift.
我正在使用斯威夫特。
采纳答案by Fogmeister
You could do this several ways. You could create a global subclass that you then subclass.
你可以通过几种方式做到这一点。您可以创建一个全局子类,然后将其子类化。
So you have your MyViewController
class with the code in and then you create subclasses of this instead of UIViewController
.
因此,您拥有MyViewController
包含代码的类,然后创建 this 的子类而不是UIViewController
.
Or you could create a category. Or a singleton. Or just a normal class with a class method. etc. that contains the code that you want to run.
或者您可以创建一个类别。或者单例。或者只是一个带有类方法的普通类。等包含您要运行的代码。
Then in your view controllers you can have the code...
然后在您的视图控制器中,您可以拥有代码...
[SomeSharedClass runThisMethod];
In Swift...
在斯威夫特...
SomeSharedClass.runThisMethod()
As David pointed out. It would be preferred to create a category or extension etc... as subclassing may lead you to having to create subclasses for several different root classes UITableViewController
, UIPageViewController
, UICollectionViewController
, etc...
正如大卫所指出的。这将是首选创建一个类或扩展等等作为子类可能会导致你不得不为几个不同的根类创建子类UITableViewController
,UIPageViewController
,UICollectionViewController
,等...
回答by Duyen-Hoa
With this use-case, I should sub-class all ViewControllers to a super class (CommonViewController):
在这个用例中,我应该将所有 ViewController 子类化为一个超类 (CommonViewController):
class CommonViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// do your common works for all sub-classes
}
}
class MyViewController : CommonViewController {
override func viewDidLoad() {
super.viewDidLoad() //call CommonViewController ViewDidLoad
//... then your work
}
}
回答by Kegham K.
I just had the opportunity to do an extension now in Swift 2 to help me send alert msgs instead of writing the code down everytime in every view controller i just write down the text string. I used UIAlertController for the newer IOS versions and UIAlertView for the old ones.
我现在有机会在 Swift 2 中做一个扩展来帮助我发送警报消息,而不是每次在每个视图控制器中都写下代码,我只是写下文本字符串。我对较新的 IOS 版本使用 UIAlertController,对旧版本使用 UIAlertView。
Just make a new .swift file and write down the extension you need for the class you want.
只需创建一个新的 .swift 文件并写下您想要的类所需的扩展名。
import UIKit
extension UIViewController{
func userNamePassAlert(theTitle: String , theMessage: String){
if #available(iOS 8.0, *) {
let userTextFieldEmptyAlert = UIAlertController(title: theTitle, message: theMessage, preferredStyle: UIAlertControllerStyle.Alert)
let userTextFieldEmptyAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: { (action) -> Void in
})
userTextFieldEmptyAlert.addAction(userTextFieldEmptyAction)
self.presentViewController(userTextFieldEmptyAlert, animated: true, completion: nil)
} else {
// Fallback on earlier versions
let userTextFieldEmptyAlert = UIAlertView()
userTextFieldEmptyAlert.title = theTitle
userTextFieldEmptyAlert.message = theMessage
userTextFieldEmptyAlert.addButtonWithTitle("Ok")
userTextFieldEmptyAlert.show()
}
}
}
And this is how i call it in my view controllers
这就是我在视图控制器中调用它的方式
else {
userNamePassAlert("Error Loging In", theMessage: "Username or Password field is empty. Try again")
}
回答by d2burke
Best way to do this, if you're really set on it, is to subclass UIViewController and put it in the init method. That way it'll run first each time a VC is instantiated. So, instead of extending UIViewController for all of your VCs, you'll extend YOUR new VC class.
最好的方法是,如果你真的设置了它,就是继承 UIViewController 并将它放在 init 方法中。这样,每次实例化 VC 时它都会首先运行。因此,不是为所有 VC 扩展 UIViewController,而是扩展新的 VC 类。