我是iOS开发的新手。我遇到了一个问题,我的用例是每次UIWindow的rootviewcontroller更改时都要进行回调。我知道rootviewcontroller中有一个rootviewcontroller.tansiondelegate委托属性,但在使用此委托后我无法获得回调。
答案 0 :(得分:1)
您可以使用[[UIApplication sharedApplication].keyWindow addObserver:self forKeyPath:@"rootViewController" options:NSKeyValueObservingOptionNew context:@"rootViewControllerChange"];
来观察财产的变化。像:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([(__bridge NSString *)context isEqualToString:@"rootViewControllerChange"] ) {
// code what you want to do...
}
}
当rootViewController被更改时,将调用方法:
[[UIApplication sharedApplication].keyWindow removeObserver:self forKeyPath:@"rootViewController" context:@"rootViewControllerChange"];
注:
当[self class] dealloc。
的实例时,不要忘记删除此观察者[[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotificationPresentedController" object:nil userInfo:nil];
有一种更愚蠢的方法:☔️
您可以使用NSNotificationCenter,当您提供控制器时,您可以
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPresentedController) name:@"PushNotificationPresentedController" object:nil];
和addObserver接收您想要回调的通知:
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.1.min.js"></script>
<meta charset=utf-8 />
<title>Morris.js Line Chart Example</title>
</head>
<body>
<div id="js_stats"></div>
<script>
Morris.Line({
element: 'js_stats',
data: [
{y: "Context 1", E: 0.0247578, D: 0.0250694, A: 0.00205555, B: 0.00445085, C: 0.011262},
{y: "Context 2", E: 0.025153150000000003, D: 0.02422775, A: 0.0020365, B: 0.0044665, C: 0.01137645},
{y: "Context 3", E: 0.0254322, D: 0.024671699999999998, A: 0.0022679, B: 0.0045135, C: 0.0114602},
{y: "Context 4", E: 0.02603472, D: 0.0249791, A: 0.00192968, B: 0.00437542, C: 0.01089924},
{y: "Context 5", E: 0.025053799999999998, D: 0.0255343, A: 0.002256, B: 0.0043565, C: 0.0118452}
],
xkey: 'y',
ykeys: ["A", "B", "C", "D", "E"],
labels: ["Strategy 1", "Strategy 2", "Strategy 3", "Strategy 4", "Strategy 5"]
});
</script>
</body>
</html>
(如果我有另一个更好的方法,我会更新我的答案。)
答案 1 :(得分:0)