Hier SyncViewController
ist mein Singleton Viewcontroller. Ich muss den aktuellen Synchronisierungsfortschritt anzeigen, sodass ich ViewController nicht jedes Mal neu starten muss, wenn der Benutzer den Synchronisierungsfortschritt sehen möchte.
in SyncViewController.m
static SyncViewController *sharedInstance;
Es folgt das Verfahren sharedInstance von Viewcontroller zu erhalten.
+ (SyncViewController *)sharedInstance
{
static dispatch_once_t once;
dispatch_once(&once, ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
sharedInstance=[storyboard instantiateViewControllerWithIdentifier:@"SyncViewController"];
});
return sharedInstance;
}
öffnen Singleton Viewcontroller aus einer anderen View-Controller:
SyncViewController *syncVC=[SyncViewController sharedInstance];
[self presentViewController:syncVC animated:YES completion:nil];
Warum würden Sie ein VC benötigen ein Singleton zu sein? – Tander
Es ist mein Requirment –
Ja, aber warum? VCs sind nicht als Singletons konzipiert und das klingt nach Missbrauch des Singleton-Paradigmas. Es gibt einen besseren Weg, um das zu tun, was Sie tun möchten. – Tander