Xcode;如何从另一个ViewController调用AppDelegate中的函数

时间:2018-08-09 16:42:07

标签: ios swift xcode function today-extension

我正在使用Swift和最新的Xcode版本。

我在AppDelegate.swift中有此代码:

func testPrint(string: String) {
    print(string)
}

我可以像这样从文件ViewController.swift成功调用该函数:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")

但是,如果我尝试从TodayViewController.swift(适用于苹果Today Extension)调用该函数,则会出现以下错误:

  

使用未声明类型的“ AppDelegate”

为什么?

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")

编辑: “今日扩展程序”和App处于不同的环境中,因此不可能。