从 App Delegate 调用 ViewController 的方法
Calling ViewController's Method from App Delegate
在我的 ViewController 收件箱中,我想从我的 App Delegate 调用方法 playAudio()。
这就是方法。
func playAudio(){
let nowPlaying = MPNowPlayingInfoCenter.defaultCenter()
let albumArtWork = MPMediaItemArtwork(image: UIImage(named: "testImage.jpg")!)
nowPlaying.nowPlayingInfo = [MPMediaItemPropertyTitle:"Sonnnngggg",
MPMediaItemPropertyArtist:"Arrrtttiiiissstttt",
MPMediaItemPropertyArtwork:albumArtWork]
audioPlayer.play()
}
我想从我的 App Delegate 调用它...
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
Inbox.playAudio()
}
一个问题是,因为我有一个 audioPlayer 和我的收件箱 ViewController 中的所有内容,而且它可能已经在播放音频,所以创建一个 Inbox 实例来调用 playAudio( ).所以,如果 Swift.
中有任何解决方法,请告诉我
谢谢,
利亚姆
在您的 viewcontroller 中,您通过
到达您的 appdelegate
(UIApplication.sharedApplication().delegate as! AppDelegate).playAudio()
您可以实例化 AppDelegate 并调用您想要的函数
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.someFunction()
但这似乎不是一个好习惯...
NSNotificationCenter:
在你想调用函数的地方使用这段代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "anotherFunction", name: "SomeNotification", object: nil)
将anotherFunction改成你要调用的函数名
在 AppDelegate 上:
NSNotificationCenter.defaultCenter().postNotificationName("SomeNotification", object: nil)
在 AppDelegate 中更改为:
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
NSNotificationCenter.defaultCenter().postNotificationName("RemotePlayPressed", object: self)
}
然后在收件箱视图控制器的 viewDidLoad 添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playAudio", name: "RemotePlayPressed", object: nil)
另加:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
收件箱视图控制器。
在我的 ViewController 收件箱中,我想从我的 App Delegate 调用方法 playAudio()。
这就是方法。
func playAudio(){
let nowPlaying = MPNowPlayingInfoCenter.defaultCenter()
let albumArtWork = MPMediaItemArtwork(image: UIImage(named: "testImage.jpg")!)
nowPlaying.nowPlayingInfo = [MPMediaItemPropertyTitle:"Sonnnngggg",
MPMediaItemPropertyArtist:"Arrrtttiiiissstttt",
MPMediaItemPropertyArtwork:albumArtWork]
audioPlayer.play()
}
我想从我的 App Delegate 调用它...
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
Inbox.playAudio()
}
一个问题是,因为我有一个 audioPlayer 和我的收件箱 ViewController 中的所有内容,而且它可能已经在播放音频,所以创建一个 Inbox 实例来调用 playAudio( ).所以,如果 Swift.
中有任何解决方法,请告诉我谢谢, 利亚姆
在您的 viewcontroller 中,您通过
到达您的 appdelegate(UIApplication.sharedApplication().delegate as! AppDelegate).playAudio()
您可以实例化 AppDelegate 并调用您想要的函数
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.someFunction()
但这似乎不是一个好习惯...
NSNotificationCenter:
在你想调用函数的地方使用这段代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "anotherFunction", name: "SomeNotification", object: nil)
将anotherFunction改成你要调用的函数名
在 AppDelegate 上:
NSNotificationCenter.defaultCenter().postNotificationName("SomeNotification", object: nil)
在 AppDelegate 中更改为:
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
NSNotificationCenter.defaultCenter().postNotificationName("RemotePlayPressed", object: self)
}
然后在收件箱视图控制器的 viewDidLoad 添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playAudio", name: "RemotePlayPressed", object: nil)
另加:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
收件箱视图控制器。