iOS: present view controller programmatically

Używam metody presentViewController:animated:completion:, aby przejść do innego kontrolera widoku.

To jest mój kod:

AddTaskViewController *add = [[AddTaskViewController alloc] init];
[self presentViewController:add animated:YES completion:nil];

Ten kod trafia do drugiego UIViewController, ale drugi kontroler jest pusty. Zawsze używałem storyboardów, ale teraz muszę to zrobić w kodzie.

Author: John Farkerson, 2013-04-22

9 answers

Jeśli używasz storyboardu, prawdopodobnie nie powinieneś używać alloc i init do tworzenia nowego kontrolera widoku. Zamiast tego spójrz na swój storyboard i znajdź segue , który chcesz wykonać; powinien mieć unikalny identyfikator (a jeśli nie, możesz ustawić go na prawym pasku bocznym).

Po znalezieniu identyfikatora dla tego segue, Wyślij swój bieżący kontroler widoku a -performSegueWithIdentifier:sender wiadomość:

[self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];

Spowoduje to, że storyboard utworzy instancję AddTaskViewController i przedstawić go w sposób, który zdefiniowałeś dla tego segue.


Jeśli, z drugiej strony, nie używasz storyboardu w ogóle, musisz dać AddTaskViewController jakiś rodzaj interfejsu użytkownika. Najczęstszym sposobem jest inicjalizacja kontrolera za pomocą nib: zamiast wywoływać init, wywołasz -initWithNibName:bundle: i podasz nazwę a .plik xib zawierający interfejs add-task:

AddTaskViewController *add = [[AddTaskViewController alloc]
                              initWithNibName:@"AddTaskView" bundle:nil];
[self presentViewController:add animated:YES completion:nil];

(istnieją inne (mniej powszechne) sposoby uzyskanie widoku powiązanego z nowym kontrolerem widoku, ale prawdopodobnie sprawi ci to najmniejszy problem z uruchomieniem.)

 44
Author: Tim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 17:11:52

Jeśli używasz Storyboard, a Twój "dodaj" viewController jest w storyboard, ustaw identyfikator "dodaj" viewcontroller w Ustawieniach, abyś mógł zrobić coś takiego:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
                                                     bundle:nil];
AddTaskViewController *add = 
           [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

[self presentViewController:add 
                   animated:YES 
                 completion:nil];

Jeśli nie masz swojego "add" viewController w storyboard lub pliku nib i chcesz utworzyć całość programowo, to appDocs mówi:

Jeśli nie możesz zdefiniować widoków w storyboardzie lub pliku nib, Nadpisz metodę loadView, aby ręcznie utworzyć instancję hierarchii widoków i przypisz go do właściwości view.

 29
Author: Leg1oneR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 18:08:05

Twój kod:

 AddTaskViewController *add = [[AddTaskViewController alloc] init];
 [self presentViewController:add animated:YES completion:nil];

Ten kod może przejść do innego kontrolera, ale dostajesz nowy kontroler viewController, a nie Kontroler twojego storyboardu, możesz zrobić tak:

AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"];
[self presentViewController:add animated:YES completion:nil];
 1
Author: klone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-30 06:05:03

Musisz ustawić Storyboard Id z Storyboard identity inspector

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];
 1
Author: Rajesh Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-17 06:54:42

Spróbuj:

NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:NULL];
 0
Author: MNIDR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-16 08:05:13

Wypróbuj ten kod:

[self.navigationController presentViewController:controller animated:YES completion:nil];
 0
Author: Oshitha Wimalasuriya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-02 10:28:19

The best way is

AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
[self presentViewController:add animated:YES completion:nil];
 0
Author: Sahidul Islam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-12 03:35:44

Oto jak możesz to zrobić w języku Swift:

let vc = UIViewController()
self.presentViewController(vc, animated: true, completion: nil)
 -1
Author: Esqarrouth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-30 01:52:57
LandingScreenViewController *nextView=[self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:^{}];
 -1
Author: MNIDR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-28 07:42:50