2012-06-04 2 views
1

먼저 프로그래밍에 대한 지식 부족에 대해 사과드립니다. 나는 학습의 길에 있으며,이 사이트가 저를 위해 큰 보완이라고 발견했습니다.메서드 실행의 마지막 행만 (Objective-C)

나는 3 개의 UITextField (thePlace, theVerb, theOutput)와 두 개의 UITextView를 가진 프로그램을 만들었고, textviews (theOutput) 중 하나는 다른 textview (theTemplate)의 텍스트를 가져 와서 일부 문자열을 텍스트 필드.

버튼을 클릭하면 아래 나열된 createStory 메소드가 트리거됩니다. 이 예외는 한 가지 예외가 있습니다. 출력의 텍스트는 문자열 'number'를 텍스트 필드의 텍스트로만 변경합니다. 그러나 'place', 'number', 'verb'를 대체 할 메소드의 순서를 변경하면 숫자가 아닌 동사 만 변경됩니다.

분명히 이것은 일종의 쉬운 수정이지만 찾을 수 없습니다. 문제 해결에 도움을 줄 수있는 사람이 있습니까?

- (IBAction)createStory:(id)sender { 
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text]; 
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text]; 
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text]; 
} 

많은 감사 // 에밀

답변

2

문제는 각 라인 theOutput.text의 내용을 덮어하고 있다는 것입니다; var1 = var2;var1의 데이터를 덮어 쓰고 var2의 내용으로 바꿉니다.

이 시도 :

- (IBAction)createStory:(id)sender 
{ 
    NSString* tempStr = [theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text]; 
    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text]; 
    theOutput.text = [tempStr stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text]; 
} 

이 메이크업 감각을합니까? :)

+0

고마워요. 그러나 밤의 잠 후 나는 그것을 더 똑똑하게 쓰는 방법을 이해했다. 매번 템플릿의 출력을 변경하는 대신 템플릿에서 처음으로 출력을 변경하고 두 번째로 첫 번째 출력의 출력을 변경하고 두 번째 출력의 세 번째 출력을 변경합니다. – Emil

+0

theOutput.text = [theTemplate.text stringByReplacingOccurrencesOfString : @ ""withString : thePlace.text]; theOutput.text = [theOutput.text stringByReplacingOccurrencesOfString : @ ""withString : theVerb.text]; theOutput.text = [theOutput.text stringByReplacingOccurrencesOfString : @ ""withString : theNumber.text]; – Emil

0
- (IBAction)createStory:(id)sender { 
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text]; 
    theTemplate.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text]; 
    theTemplate.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text]; 
} 

이 작동합니까?

+0

그건 작동하지 않습니다. 마지막 두 줄은'theTemplate.text'를 변경합니다. 이것은 아마도'theOutput.text'에 영향을 미치지 않습니다. –

+0

wups. 그것은 내가 의도 한 바가 아닙니다 : P –

0

먼저 thePlace.texttheVerb.text이 빈 값을 반환하지 않는지 확인하십시오. 그러나 이것은 당신의 문제가 아닙니다.

NSLog(@"thePlace: %@",thePlace.text); 
NSLog(@"theVerb: %@",theVerb.text); 

귀하의 코드가 있어야한다 :

- (IBAction)createStory:(id)sender { 

    NSString * output = theTemplate.text; 

    output = [output stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text]; 
    output = [output stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text]; 
    output = [output stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text]; 

    theOutput.text = output; 
} 
+1

'theOutput.text'에'output'을 지정하지 않았습니다. –

+0

아, 감사합니다. – WrightsCS

+0

또한 위의 설명 중 하나에서 설명한 더 쉬운 해결책을 찾았습니다. 시간 내 줘서 고마워! – Emil

관련 문제