2013-08-20 4 views
23

최근에 Xcode에서 MainStoryboard.storyboard로 작업 해 보았습니다. 지금까지는 꽤 좋았습니다. 전에 사용하지 않은 이유가 궁금합니다. 어떤 코드로 노는 동안 나는 장애물에 부딪 쳤고 이것을 해결하는 방법을 모른다.스토리 보드 및 사용자 정의

나는 ALLOC 나는 이런 식으로 뭔가 할 것이다 (필자는 ViewControllers 클래스에서 선언 된 사용자 정의 초기화로) 새로운의 ViewController init을 때 :

:

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

그런 다음 그 후 내가 좋아하는 뭔가를 할 수를

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

스토리 보드로 작업 할 때 독립형 ViewController로 전환하려면 식별자가 필요하다는 것을 알게되었습니다.

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

스토리 보드를 사용하면서 myViewController에 대한 사용자 정의 초기화를 계속 사용할 수 있습니까?

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; 
} 

답변

17

난 그냥 사용자 정의 데이터 로딩을하는 방법을 만들 것입니다 :

는 확인 단지 같은 것을 할 것입니다. 모든 initWithCustomData 방법은 하나 개의 인스턴스 변수를 설정 한 경우

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

, 당신은 단지 수동으로 설정해야합니다 (더 inits 사용자 정의 또는 별도의 방법이 필요 없음) :

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

감사합니다. Apple이 Storyboards로 적절한 사용자 정의'init' 메소드를 생성하는 방법을 제공하지 않는다는 사실에 미친 듯이 보입니다. – jowie

+2

"왜 모든 initWithCustomData 메소드가 하나의 인스턴스 변수를 설정했는지 수동으로 설정해야합니다 (사용자 정의 inits 나 별도의 메소드가 필요하지 않습니다)"라는 이유가 확실하지 않습니다. 어떤 독자에게이 진술을 단순한 의견으로 간주하고 따라야한다는 규칙이 아니라고 생각하도록 조언 할 것이라고 말하는 Apple의 문서는 없습니다. iOS에서 초기화 프로그램을 많이 사용하면 단 하나의 인수 만 사용할 수 있습니다. – zumzum

+0

@zumzum Apple은 예제 코드에서 'prepareForSegue : sender :'메소드에서 변수 설정 연습을 사용합니다. [EKReminderSuite]의 'AddTimedReminder.m'파일 (https://developer.apple.com/library/prerelease/ios/samplecode/EKReminderSuite/Introduction/Intro.html#//apple_ref/doc/uid/TP40015203- Intro-DontLinkElementID_2) 예제 프로젝트를 보여줍니다. –

16

당신은 -init 방법의 ViewController를 인스턴스화 할 수 있습니다 .

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

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

    if(self) 
    { 
    //default initialization 

    } 
    return self; 
} 

와 사용자 정의 초기화 방법

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

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

    return self; 
} 
+1

Swift에서이 작업을 수행 할 수 있다고 생각하지 않습니다 ... –

+0

그래, ObjC에서는 작동하지만 Swift에서는 작동하지 않을 수 있습니다. – Pranshu

2

내 버전 :

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

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

    if(self) 
    { 
    //default initialization 

    } 
    return self; 
} 

... 하나 초기화)