以编程方式删除子视图(Removing subviews programmatically)

编程入门 行业动态 更新时间:2024-10-24 10:23:54
以编程方式删除子视图(Removing subviews programmatically)

我在appdelegate中的didReceiveRemoteNotification:方法中得到了这段代码

我想知道如何使用延迟删除此子视图,假设子视图显示来自推送通知的信息,但我希望它们在3.5秒后消失(delay2)

谢谢!

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"remote notification: %@",[userInfo description]); NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; NSString *alert = [apsInfo objectForKey:@"alert"]; NSLog(@"Received Push Alert: %@", alert); NSString *sound = [apsInfo objectForKey:@"sound"]; NSLog(@"Received Push Sound: %@", sound); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); NSString *badge = [apsInfo objectForKey:@"badge"]; NSLog(@"Received Push Badge: %@", badge); application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue]; // Create the UILabel instance CGRect myFrame = CGRectMake(0, 20, 320, 50); UIView *myView = [[UIView alloc] initWithFrame:myFrame]; myView.backgroundColor = [UIColor blueColor]; [self.window addSubview:myView]; [myView release]; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)]; [aLabel setText:alert]; [self.window addSubview:aLabel]; [aLabel release]; NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio play]; //SONAR// [self performSelector:@selector(delay2) withObject:nil afterDelay:3.5]; } -(void)delay2 { NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio play]; }

I got this code inside my didReceiveRemoteNotification: method in appdelegate

I would like to know how can I remove this subviews using a delay, it is supposed the subviews show info from a push notification but I want them to disappear after 3.5 seconds(delay2)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"remote notification: %@",[userInfo description]); NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; NSString *alert = [apsInfo objectForKey:@"alert"]; NSLog(@"Received Push Alert: %@", alert); NSString *sound = [apsInfo objectForKey:@"sound"]; NSLog(@"Received Push Sound: %@", sound); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); NSString *badge = [apsInfo objectForKey:@"badge"]; NSLog(@"Received Push Badge: %@", badge); application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue]; // Create the UILabel instance CGRect myFrame = CGRectMake(0, 20, 320, 50); UIView *myView = [[UIView alloc] initWithFrame:myFrame]; myView.backgroundColor = [UIColor blueColor]; [self.window addSubview:myView]; [myView release]; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)]; [aLabel setText:alert]; [self.window addSubview:aLabel]; [aLabel release]; NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio play]; //SONAR// [self performSelector:@selector(delay2) withObject:nil afterDelay:3.5]; } -(void)delay2 { NSString *path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"mp3"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio play]; }

最满意答案

您可以使用UIView的removeFromSuperview实例方法。 在向所有人添加窗口集标记的视图时。 在delay2方法中,使用tag&removeFromSuperview方法删除它们。

CGRect myFrame = CGRectMake(0, 20, 320, 50); UIView *myView = [[UIView alloc] initWithFrame:myFrame]; myView.backgroundColor = [UIColor blueColor]; [myView setTag:999]; [self.window addSubview:myView]; [myView release]; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)]; [aLabel setText:alert]; [aLabel setTag:999]; [self.window addSubview:aLabel]; [aLabel release]; -(void)delay2 { for(UIView *subView in window.subviews) { if(subView.tag == 999) [subView removeFromSuperview]; }

}

You can use removeFromSuperview instance method of UIView. While adding views on window set tag to all. And in delay2 method remove them using tag & removeFromSuperview method.

CGRect myFrame = CGRectMake(0, 20, 320, 50); UIView *myView = [[UIView alloc] initWithFrame:myFrame]; myView.backgroundColor = [UIColor blueColor]; [myView setTag:999]; [self.window addSubview:myView]; [myView release]; UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 300, 20)]; [aLabel setText:alert]; [aLabel setTag:999]; [self.window addSubview:aLabel]; [aLabel release]; -(void)delay2 { for(UIView *subView in window.subviews) { if(subView.tag == 999) [subView removeFromSuperview]; }

}

更多推荐

本文发布于:2023-07-27 17:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   方式   Removing   subviews   programmatically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!