iOS代码用倒计时关闭视图

时间:2014-11-18 12:03:15

标签: ios objective-c uiview

我正在试验iOS 8 Objective-c项目,以便在呈现viewDidLoad时立即执行segue到视图控制器。

然后,这个最近呈现的新视图应该被30秒倒计时结束,以返回初始视图控制器。

用倒计时解雇视图的代码是什么样的?倒计时界面不是必需的,只需要背景代码。

我的初始视图控制器名为ConfViewController,将在viewDidLoad和BubbleViewController上执行一个segue,这应该显示30秒然后再次被解散到第一个ConfViewController。

@implementation ConfViewController

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
 [super viewDidLoad];

BubbleViewController

@interface BubbleViewController ()

@end

@implementation MessagesViewController {
  UIVisualEffectView *blurEffectView;
}

- (void)viewDidLoad
{
  self.dataSource = self;
  self.delegate = self;

  [super viewDidLoad];

Thnx和欢呼!

3 个答案:

答案 0 :(得分:0)

将此代码放入您的气泡视图控制器viewDidLoad或类似内容,它将在30秒后解除自身:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self dismissViewControllerAnimated:YES completion:nil];
});

答案 1 :(得分:0)

一段时间后执行任何代码的快捷方法是使用此代码段:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //code to be executed after a specified delay });

您应该在演示者或提供的控制器viewDidLoad中将此代码放在哪里。

你应该如何解雇它取决于你呈现它的方式。

答案 2 :(得分:0)

BubbleViewController's viewDidLoad方法

中添加以下代码行
//countdown can be according to requirement
[self performSelector:@selector(dissmissView) withObject:nil afterDelay:30];

现在描述dissmissView方法并添加这些方法。

-(void)dissmissView
{
    if (self.navigationController.presentingViewController)
    {
       //presented As ModaL
       [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if(self.navigationController)
    {
       //Pushed in navigation controller stack
       [self.navigationController popViewControllerAnimated:YES];
    }
    else //added as subView
    {
       [self.view removeFromSuperview];
    }
}