2013-08-20 29 views
23

Son zamanlarda Xcode içindeki MainStoryboard.storyboard ile çalışmayı denedim ve şu ana kadar oldukça iyi gidiyor ve neden daha önce hiç kullanmadığımı merak ediyorum. Bazı kodlarla oynarken bir engele çarptım ve bunu nasıl çözeceğimi bilmiyorum.Storyboard ve özel init

Ben Alloc ve böyle bir şey yapacağını (ı ViewControllers sınıfta bildirilen özel bir init ile) yeni bir ViewController init zaman:

:

ViewController *myViewController = [[ViewController alloc] initWithMyCustomData:myCustomData]; 

ondan sonrada ben böyle bir şey yapabileceğini

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

Bir storyboard ile çalışırken, bağımsız bir ViewController uygulamasına geçmenin bir Tanımlayıcı gerektirdiğini öğrendim.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; 
[self presentViewController:myViewController animated:YES completion:nil]; 

Hikaye panosundan yararlanırken myViewController için özel başlatmamı nasıl kullanabilirim?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; 
myViewController.customData = myCustomData; 
[self presentViewController:myViewController animated:YES completion:nil]; 




//MyViewController.m 
- (id) initWithMyCustomData:(NSString *) data { 
if (self = [super init]) { 
    iVarData = data; 
} 
return self; 
} 

cevap

17

Sadece özel veri yükleme yapan bir yöntem yaratacak:

Tamam sadece böyle bir şey yapmak mı. Tüm initWithCustomData yöntem bir örnek değişkeni ayarlanır yoksa

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; 
[myViewController loadCustomData:myCustomData]; 
[self presentViewController:myViewController animated:YES completion:nil]; 

, sadece el ile ayarlamanız gerekir (hayır inits özel veya ekstra yöntemler gerekli):

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; 
myViewController.iVarData = myCustomData; 
[self presentViewController:myViewController animated:YES completion:nil]; 
+14

teşekkürler. Apple'ın Storyboard'ları ile uygun bir özel 'init 'yöntemi oluşturmamanın bir çılgınlığı olmadığını çılgınca görüyor. – jowie

+2

Neden birinin açıklanacağından emin değilim: "Tüm initWithCustomData yönteminiz bir örnek değişkenini ayarladıysa, bunu yalnızca manuel olarak ayarlamanız gerekir (özel girişler veya fazladan yöntemler gerekmez)". Apple'ın herhangi bir okuyucuyu bu ifadeyi sadece bir fikir değil, izleyecek bir kural olarak değerlendirmesini tavsiye eden bir belge yoktur. IOS’ta çok sayıda başlatıcı bile tek bir argüman alır. – zumzum

+0

@zumzum Apple, örnek kodlarında 'prepareForSegue: sender:' yöntemindeki değişkenleri ayarlama pratiğini kullanır. [EKReminderSuite] 'de' AddTimedReminder.m 'dosyasına bakın (https://developer.apple.com/library/prerelease/ios/samplecode/EKReminderSuite/Introduction/Intro.html#//apple_ref/doc/uid/TP40015203- Intro-DontLinkElementID_2) örnek bir örnek proje. –

16

Sen -init yönteminde viewcontroller örneğini .

- (instancetype)init 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 

    self = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"]; 

    if(self) 
    { 
    //default initialization 

    } 
    return self; 
} 

ve özel init yönteminde

- (instancetype)initWithImages:(NSArray *)images 
{ 
    self = [self init]; 

    if(self) 
    { 
    self.images = images; 
    } 

    return self; 
} 
+1

Ben bunu Swift'de yapabileceğinizi sanmıyorum ... –

+0

Evet, ObjC için çalışabilir, ancak Swift'de çalışmayabilir. – Pranshu

2

Benim versiyon:

- (instancetype)initWithData (NSArray *)someData 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 

    self = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"]; 

    if(self) 
    { 
    //default initialization 

    } 
    return self; 
} 

... bir başlatıcı;)

İlgili konular