应用程序进入后台确定当前的ViewController

时间:2015-06-18 21:07:01

标签: ios swift uiviewcontroller notifications

我目前正在开发一款iOS应用,并希望向通知中心添加通知,当且仅当用户在离开应用时正在查看某个视图控制器时。该应用程序有一个倒计时器,如果他们正在查看倒计时器,然后去发送文本或任何其他应用程序,我想在计时器为0时发送通知。但如果他们在另一个视图控制器上我无需发送通知。任何帮助都会非常感谢。

1 个答案:

答案 0 :(得分:0)

在您要检测应用程序转到后台的 public static PlotModel ExampleScatterSeriesPlot() { var plotModel1 = new PlotModel(); plotModel1.Subtitle = "The scatter points are added to the Points collection."; plotModel1.Title = "ScatterSeries"; var linearAxis1 = new LinearAxis(); linearAxis1.Position = AxisPosition.Bottom; plotModel1.Axes.Add(linearAxis1); var linearAxis2 = new LinearAxis(); plotModel1.Axes.Add(linearAxis2); var scatterSeries1 = new ScatterSeries(); scatterSeries1.Points.Add(new ScatterPoint(0.667469348137951, 0.701595088793707)); scatterSeries1.Points.Add(new ScatterPoint(7.74765135149828, 5.11139268759237)); scatterSeries1.Points.Add(new ScatterPoint(7.97490558492714, 8.27308291023275)); scatterSeries1.Points.Add(new ScatterPoint(1.65958795308116, 7.36130623489679)); scatterSeries1.Points.Add(new ScatterPoint(2.6021636475819, 5.06004851081411)); scatterSeries1.Points.Add(new ScatterPoint(2.30273722312541, 3.87140443263175)); scatterSeries1.Points.Add(new ScatterPoint(2.15980615101746, 0.208108848989061)); scatterSeries1.ActualPoints.Add(new ScatterPoint(0.667469348137951, 0.701595088793707)); scatterSeries1.ActualPoints.Add(new ScatterPoint(7.74765135149828, 5.11139268759237)); scatterSeries1.ActualPoints.Add(new ScatterPoint(7.97490558492714, 8.27308291023275)); scatterSeries1.ActualPoints.Add(new ScatterPoint(1.65958795308116, 7.36130623489679)); scatterSeries1.ActualPoints.Add(new ScatterPoint(2.6021636475819, 5.06004851081411)); scatterSeries1.ActualPoints.Add(new ScatterPoint(2.30273722312541, 3.87140443263175)); scatterSeries1.ActualPoints.Add(new ScatterPoint(2.15980615101746, 0.208108848989061)); plotModel1.Series.Add(scatterSeries1); return plotModel1; } 内,您需要在{UIViewController时注册UIApplicationWillResignActiveNotificationUIApplicationWillTerminateNotification通知NSNotificationCenter 1}}被称为。

viewWillAppear

当此屏幕回调您处理通知的方法时,请安排NSNotificationCenter .defaultCenter() .addObserver(self, selector: Selector("callBack"), name: UIApplicationWillResignActiveNotification, object: nil) NSNotificationCenter .defaultCenter() .addObserver(self, selector: Selector("callBack"), name: UIApplicationWillTerminateNotification, object: nil) 在当前时间触发,再加上计时器计数。您可以UILocalNotification使用init(timeIntervalSinceNow:)进行此操作。

NSDate

然后以观察者身份删除var fireDate = NSDate(timeIntervalSinceNow: 25) var localNotification = UILocalNotification() localNotification.fireDate = fireDate UIApplication .sharedApplication() .scheduleLocalNotification(localNotification)

UIViewController