xcode 如何自动从一个视图控制器移动到另一个视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45407595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move from one view controller to another automatically?
提问by Srinidhi Kowshik
I want to switch to another view controller after my app is loaded after some seconds, how to do that ?
我想在几秒钟后加载我的应用程序后切换到另一个视图控制器,该怎么做?
回答by Harshal Valanda
Try this
尝试这个
You can perform segue, navigate and present next view controller in the block
您可以在块中执行转场、导航和呈现下一个视图控制器
Add this in ViewDidLoad
, ViewWillAppear
添加这个ViewDidLoad
,ViewWillAppear
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
self.performSegue(withIdentifier: "leadingToTutorial", sender: self)
})
回答by Jaydip
In Swift 3.0,you used Time to move one ViewControllerto second ViewController. here I have create code so watch it.
在Swift 3.0 中,您使用 Time 将一个ViewController移动到第二个ViewController。我在这里创建了代码,所以请注意。
import UIKit
导入 UIKit
class ViewController: UIViewController {
var gameTimer: Timer! //Timer object
override func viewDidLoad() {
super.viewDidLoad()
gameTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timeaction), userInfo: nil, repeats: true)
}
//Timer action
func timeaction(){
//code for move next VC
let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
gameTimer.invalidate()//after that timer invalid
}
}
I hope It's work.
我希望这是工作。
回答by Er.Gopal Vasani
when viewDidLoad Method of your First ViewControllers will Execute , you can define there your code For Move to Another ViewController , For Some Second delay you can use
当您的第一个 ViewControllers 的 viewDidLoad 方法将执行时,您可以在那里定义您的代码 For Move to another ViewController ,对于一些第二个延迟,您可以使用
For Delay of Some Seconds
延迟几秒
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// your code here For Pushing to Another Screen
}
here you can define how much time you want as a delay , in example i mentioned 0.1 , you can define as per your requirements
在这里你可以定义你想要多少时间作为延迟,在我提到的例子中 0.1 ,你可以根据你的要求定义
回答by Hosein Abbaspoor
Use the Timer
使用 Timer
Class name {
private var timer: Timer?
fun viewDidLoad() {
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(yourMethod), userInfo: nil, repeats: false)
}
objc func yourMethod() {
performSegue(withIdentifier:Sender:)
}
}
回答by Paras Gorasiya
In your view's viewDidLoad()
method you can add following line at the end of the method
在您的视图viewDidLoad()
方法中,您可以在方法的末尾添加以下行
[self performSelector:@selector(goNext:)withObject:nil afterDelay:5.0];
And inside goNext:
method you can add
goNext:
你可以在里面添加方法
[self.navigationController pushViewController:vc animated:YES];
to perform navigation and go to next view.
执行导航并转到下一个视图。