如何从 Appdelegate 调用另一个 ViewController 的方法?
How to call a method of another ViewController from Appdelegate?
我需要从我的 appdelegate 执行一个来自 viewcontroller.
的方法
拜托,谁能告诉我如何轻松地从我的 appdelegate 调用我需要的任何方法?
很多关于这个论点的问题,但是 none 其中 useful/complete/right,所以请避免 post URL 到其他主题:我已经检查了所有问题但我真的无法在 Swift 中找到关于这样做的任何准确答案。 :)
您可以通过通知来做到这一点,只需将观察者添加到您的 viewcontroller,post 来自您的应用程序委托的通知,该观察者将捕获它并且 运行 您指定的函数.
本教程应该可以帮助您入门:https://www.codefellows.org/blog/how-to-implement-an-nsnotification-observer-in-swift
这取决于您如何安排视图控制器,但这里是一个简单的 iPhone master/detail 项目的示例。
let root : UINavigationController = self.window!.rootViewController! as UINavigationController
let master : MasterViewController = root.topViewController as MasterViewController
println("\(master.description)")
我这样做的方法是搜索我想要的视图控制器,从 AppDelegate 的 var window: UIWindow?
开始,然后深入直到找到它。我最初尝试实现 NSNotification
但在 swift 上不推荐这样做(我认为计算 属性 是一个很好的替代品,但在这种情况下不起作用)。
这就是我在 ViewController 之上使用 NavigationController 为基于选项卡的应用程序所做的:
if let rootViewController = self.window?.rootViewController as? UITabBarController
if let viewControllers = rootViewController.viewControllers {
for navigationController in viewControllers {
if let yourViewController = navigationController.topViewController as? YourCustomViewController {
if yourViewController.hasSomeFlag {
yourViewController.theMethod()
}
break
}
}
}
}
试试这个
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
return true
}
func customMethod()
{
}
}
从 ViewController
调用自定义方法
class ViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
appDelegate.customMethod()
}
我需要从我的 appdelegate 执行一个来自 viewcontroller.
的方法拜托,谁能告诉我如何轻松地从我的 appdelegate 调用我需要的任何方法?
很多关于这个论点的问题,但是 none 其中 useful/complete/right,所以请避免 post URL 到其他主题:我已经检查了所有问题但我真的无法在 Swift 中找到关于这样做的任何准确答案。 :)
您可以通过通知来做到这一点,只需将观察者添加到您的 viewcontroller,post 来自您的应用程序委托的通知,该观察者将捕获它并且 运行 您指定的函数.
本教程应该可以帮助您入门:https://www.codefellows.org/blog/how-to-implement-an-nsnotification-observer-in-swift
这取决于您如何安排视图控制器,但这里是一个简单的 iPhone master/detail 项目的示例。
let root : UINavigationController = self.window!.rootViewController! as UINavigationController
let master : MasterViewController = root.topViewController as MasterViewController
println("\(master.description)")
我这样做的方法是搜索我想要的视图控制器,从 AppDelegate 的 var window: UIWindow?
开始,然后深入直到找到它。我最初尝试实现 NSNotification
但在 swift 上不推荐这样做(我认为计算 属性 是一个很好的替代品,但在这种情况下不起作用)。
这就是我在 ViewController 之上使用 NavigationController 为基于选项卡的应用程序所做的:
if let rootViewController = self.window?.rootViewController as? UITabBarController
if let viewControllers = rootViewController.viewControllers {
for navigationController in viewControllers {
if let yourViewController = navigationController.topViewController as? YourCustomViewController {
if yourViewController.hasSomeFlag {
yourViewController.theMethod()
}
break
}
}
}
}
试试这个
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
return true
}
func customMethod()
{
}
}
从 ViewController
调用自定义方法class ViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
appDelegate.customMethod()
}