Perform Segue in Viewdidload

Perform Segue in ViewDidLoad

You can't use performSegue() from within viewDidLoad(). Move it to viewDidAppear().

At viewDidLoad() time, the current view isn't even attached to the window yet, so it's not possible to segue yet.

Perform Segue From viewDidLoad()

Need to perform segue at right place.you are tried to loading another view before first one in hierarchy.So viewDidAppear is called you have a fully loaded view to modify.

   - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSegueWithIdentifier:@"myNextControllerSegue" sender:nil];
}

To remove that flickering, just hide the view in your viewWillApear method.

otherwise as quick search you can do that into main thread also like below

dispatch_async(dispatch_get_main_queue(), { () -> Void in   
//perform segue
})

Perform Segue on ViewDidLoad

I answered a similar question where the developer wanted to show a login screen at the start. I put together some sample code for him that can be downloaded here. The key to solving this problem is calling things at the right time if you want to display this new view controller, you will see in the example you have to use something like this

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];

[self presentModalViewController:vc animated:YES];
}

I also have an explanation of how segues and storyboards work that you can see here

perform segue in viewDidLoad() without navigation controller in Swift

You can use this code for navigation.

    let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ConnectionCheckToMain") as! UIViewController;
self.presentViewController(vc, animated: true, completion: nil)

Not performing segue

You need to call this self.performSegue method in dispatch_async block:

override func viewDidLoad() {
super.viewDidLoad()

DispatchQueue.main.async {
self.performSegue(withIdentifier: "seg", sender: self)
}
}

That's because viewDidLoad() runs in background thread and all UI updates and transitions must be done on Main Thread.



Related Topics



Leave a reply



Submit