如果应用在前台接收到通知,我会尝试使用Toast显示横幅。
简单的案例没有任何问题。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIView *parentView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
[parentView makeToast:@"toast"];
}
但是,当我尝试显示自己的自定义视图时,EXC_BAD_ACCESS
方法出现错误showToast
错误:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.topController.view.frame.size.width, 80)];
UIView *parentView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
bannerView.backgroundColor = [UIColor redColor];
[parentView showToast: bannerView duration:3.0 position:CSToastPositionTop completion:nil];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
方法是编程此类行为的危险场所吗?强烈引用parentView似乎不能正常工作,如何确定哪个对象被过度释放?
答案 0 :(得分:0)
您可以直接在appdelegate
窗口上显示吐司,而不是获取父视图,该视图位于所有视图的顶层。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
[self.window makeToast:@"This is a piece of toast."];
}
}