Good night everyone, I needed a switch to another ViewController through the code, usually I connected the button to the UIViewController directly via Ctrl, (modal). Now the program first opens a check on the connection to the Internet and if everything is ok, the main UIViewController opens with the program. But it was not there, for the second day I’m looking for how to correctly put the transition from one to another View. The only thing that tried to go anywhere is this code:

import "ViewController" ... ViewController *test = [[ViewController alloc] init]; [self presentViewController:test animated:YES completion:nil]; 

But he opened a black background, not a view. I also tried

 ViewController *test = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; [self.navigationController pushViewController:test animated:YES]; 

Nothing happens at all, I tried many more different ways, but these were in almost all articles. Help please, what is the error. I need any transition at all, without any problems. Thank you very much in advance.

  • Apparently everyone who knows Objective-C has gone to bed, I hope to answer tomorrow) - SuperPonchik

2 answers 2

Do you happen to work with Storyboard there? If yes, then you need another way to initialize your controller to take it from the storyboard:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; // Имя вашей storyboard, обычно так и есть - MainStoryboard. ViewController *testViewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; // identifier вам нужно, чтобы был проставлен в Storyboard. NSLog(@"Проверяем, удалось ли получить контроллер: %@", testViewController); [self.navigationController pushViewController:testViewController animated:YES]; 

PS Your question wording is mysterious - there are constructions in it, written by persons, both masculine and feminine. And what are these times in which we live? ;)


This is how the Identifier is affixed:

alt text

  • I’m writing from a crooked Safari, put the spellchecking module) Now this hat comes out) Thanks a lot for the answer, now I’ll try) - SuperPonchik
  • f3.s.qip.ru/MQ9HpDCx.png crash screenshot, what did you do wrong? : C And here’s how I put it on the Storyboard: f4.s.qip.ru/MQ9HpDCy.png - SuperPonchik
  • Hat)) OK)) I added the answer about crash - you should set the Identifier for the controller so that - [UIStoryboard instantiateViewControllerWithIdentifier:] knew who to catch it)) - Stanislav Pankevich
  • I did everything as they said and showed, now there are no errors, the log comes, f4.s.qip.ru/MQ9HpDCR.png , but the transition does not occur, something is wrong?)) // And you can give out your program promo?) then it is in my top of the Russian AppStore, 99p worth, I would give you for such help: 3 - SuperPonchik
  • > And you can give out your program's promo?) If you want, you can get to me through the info in your profile, I will not object))> but the transition does not happen, something is wrong?)) And what happens? - Stanislav Pankevich

Swift

Implementation without segue.

We write in UIViewController

 static func storyboardInstance() -> VPTestViewController? { let storyboard = UIStoryboard(name: "Main", bundle: nil) return storyboard.instantiateViewController(withIdentifier: "VPTestViewController") as? VPTestViewController } 

Further, where it is necessary to call the controller, we write:

 let testVC = VPTestViewController.storyboardInstance() testVC.someText = "Test Text" 

Further, as conceived by logic:

1) or through the navigation controller

 navigationController?.pushViewController(testVC!, animated: true) 

2) or modally

 self.present(testVC!, animated: true, completion: nil) 

Well, of course, we don’t forget to specify in our controller in the storyboard

enter image description here