2016-06-07 139 views
-1

我必须验证一些内容才能决定是继续执行viewDidload()句子还是执行segue,但是当我调试代码时,在performSegueWithIdentifier调用之后执行即将到来的句子继续,用户可以看到当前ViewController的一些UI,我想在该ViewController中进行验证,而不是在其父项中进行验证。performSegueWithIdentifier不会立即执行

下面是我的一些代码:

override func viewDidLoad() { 
    super.viewDidLoad() 
    let questionD = self.question![DECRIPTION] 
    let type = self.question![TYPE] as? String; 
    if type == "60" { 
     performSegueWithIdentifier("showCameraSegue", sender: self) 
    } 
    setBackBtn() 

    self.lblResponse.text = questionD as? String; 

    lblResponse.backgroundColor = UIColor.whiteColor() 
... 
} 

回答

1

你可以的延迟也许半秒后执行的代码行,以便执行代码之前新的视图控制器会显示。

你可能会想尝试实现这个地方..

func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) { 
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))) 
    dispatch_after(time, dispatch_get_main_queue(), block) 
} 

,然后调用它像这样:

runAfterDelay(delay: 0.5, { 

    setBackBtn() 

    self.lblResponse.text = questionD as? String; 

    lblResponse.backgroundColor = UIColor.whiteColor() 
    ... 
}) 

这不会立即运行SEGUE像你问的...但它会让所有其他的东西在你继续运行后运行一点点

或者如果你使用的是导航控制器,当你知道新的视图控制器是presen的时候你可以执行代码ting:

class MyViewControllerSubclass : UIViewController, UINavigationControllerDelegate { 

func viewDidLoad() { 
    self.navigationController.delegate = self 
} 

func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) { 
    //put your code here 
} 
}