Jak programowo wywołać kontroler widoku?

Przejrzałem wszystkie tutoriale, które mogę znaleźć na ten temat, i nadal nie mam odpowiedzi. Muszę zadzwonić do innego widoku z kodu. Używam UIStoryboards. Zmieniałem widok wiele razy przez przeciąganie sterowania z UIButtons, ale teraz musi być z kodu. Próbuję wywołać stronę informacyjną z menu głównego, jeśli jest to pierwszy raz, gdy użytkownik otworzył aplikację. Nie mogę jednak znaleźć sposobu na zmianę widoków z kodu. Wszystkie moje widoki są kontrolowane przez te same pliki ( ViewController2 ). identifier mojego głównego menu to Viewcontrollerinfo, A identifierstrony info to ViewControllerInfo. Najpierw próbowałem tego:

[ViewControllerMain presentViewController: ViewControllerInfo 
                                 animated:YES 
                               completion: NULL];

Potem próbowałem zrobić różne UIViewControllers dla każdego i mówię:

[ViewController2 presentViewController: ViewController 
                              animated:YES 
                            completion: NULL];
Nie zadziałało. Dla pierwszego jest napisane:

Użycie nierejestrowanego identyfikatora ViewControllerMain.

W drugim jest napisane:

Nieoczekiwana nazwa interfejsu 'ViewController': oczekiwany identyfikator.

Co mogę zrobić?
Author: Alex Cio, 2013-04-21

8 answers

Aby utworzyć kontroler widoku:

UIViewController * vc = [[UIViewController alloc] init];

Aby wywołać kontroler widoku (musi być wywołany z innego kontrolera widoku):

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

Po pierwsze, użyj nil zamiast null.


Ładowanie kontrolera widoku ze storyboardu:

NSString * storyboardName = @"MainStoryboard"; 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];

Identifier kontrolera widoku jest równa nazwie klasy kontrolera widoku lub identyfikator Storyboard, który możesz przypisać w Inspektorze tożsamości swojego storyboardu.

 125
Author: 190290000 Ruble Man,
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
2014-06-10 04:49:09

Musisz utworzyć instancję kontrolera widoku ze storyboardu, a następnie pokazać go:

ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];

Ten przykład zakłada, że masz Kontroler nawigacyjny, aby powrócić do poprzedniego widoku. Można oczywiście również użyć presentViewController: animated: completion:. Głównym punktem jest stworzenie instancji storyboard kontrolera widoku docelowego za pomocą identyfikatora kontrolera widoku docelowego.

 20
Author: tigloo,
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-21 21:00:32

Swift

To pobiera kontroler widoku z storyboardu i przedstawia go.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)

Zmień nazwę storyboardu, nazwę kontrolera widoku i ID kontrolera widoku.

 16
Author: Suragch,
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-12-16 04:53:51

Możesz wywołać ViewController w ten sposób, jeśli chcesz z NavigationController

Tutaj wpisz opis obrazka

1.In bieżący ekran: Wczytaj nowy ekran

VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];

2.1 w załadowanym widoku: dodaj poniżej .plik h

@interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>

2.2 w załadowanym widoku: dodaj poniżej .plik m

  @implementation VerifyExpViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.delegate = self;
    [self setNavigationBar];
}
-(void)setNavigationBar
{
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}

-(void)onBackButtonTap:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
    //todo for save button
}

@end

Mam nadzieję, że to się komuś przyda:)

 5
Author: swiftBoy,
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-03-21 12:18:41

Można to zrobić na dwa sposoby:

1, Utwórz segue do swojego kontrolera ViewController w swoim Storyboardzie, jak wyjaśniono w mojej odpowiedzi tutaj: Jak wykonać segue, który nie jest związany z wejściem użytkownika w iOS 5?

2, Podaj swój ViewController i identyfikator i wywołaj go za pomocą kodu w mojej odpowiedzi tutaj: wywołaj storyboard scene programowo (bez potrzeby segue)?

 3
Author: Darren,
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-05-23 12:26:10

Główna logika za tym stoi_,

NSString * storyboardIdentifier = @"SecondStoryBoard";

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];

UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];

[self presentViewController:UIVC animated:YES completion:nil];
 1
Author: Vaibhav Shiledar,
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-08-30 11:06:56

Zaimportuj klasę kontrolera widoku, którą chcesz wyświetlić i użyj następującego kodu

KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];
 0
Author: vijeesh,
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-08-24 07:40:11
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil];
            AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"];
            //            [self presentViewController:controller animated:YES completion:nil];

        UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (topRootViewController.presentedViewController)
        {
            topRootViewController = topRootViewController.presentedViewController;
        }

        [topRootViewController presentViewController:controller animated:YES completion:nil];
 0
Author: suresh,
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-01-28 19:10:57