2014-10-13 3 views
0

내가 뭘하고 싶은지 : 배열의 항목이 레이블에 표시됩니다. 배열의 각 항목에는 자체 레이블이 있습니다. 결국 나는 더 많은 항목을 추가하고 싶습니다.배열 및 추가

그리고 thisArray의 인덱스 0을 해당 배열의 인덱스 0에 추가 할 수 있도록하십시오. 라벨에 표시; thisArray의 인덱스 1에있는 객체를 해당 Array의 인덱스 1에있는 객체에 추가 한 다음 결과를 별도의 레이블 등과 같이 표시 할 수 있습니다. 버튼 클릭으로 표시됩니다.

나는 이것을 구현하는 데 많은 어려움을 겪고 있으며, 나는 그것을 만드는 것보다 훨씬 더 간단하다고 느낀다. 모든 통찰력은 appreicated 것입니다!

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 


self.steakArray = [[NSArray alloc] initWithObjects: @"800", @"50", nil]; 

//NSString *name; 
NSString *calories = [_steakArray objectAtIndex:0]; 
NSString *fat = [_steakArray objectAtIndex:1]; 



} 



- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 


//this doesn't work, this is what I want to have happen though. 
//click button, show this in label. 
- (IBAction)steak:(id)sender { 

self.infoLabel.text = calories, fat; 

} 
@end 
+2

왜 칼로리 카운트가 문자열로 유지되는지 티파니? – rmaddy

+1

내가 뭘하는지 전혀 모르니까. – Kate

+0

"추가"라고 말하면 실제로 숫자 추가를 의미합니까, 아니면 문자열과 같이 추가 된 것을 의미합니까? –

답변

0

은이 문서를 볼 필요가 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

당신은 형식으로 문자열을 사용할 수 있습니다

NSString *string1 = [NSString stringWithFormat:@"A string: %@, a float: %1.2f", 

               @"string", 31415.9265]; 

// string1 is "A string: string, a float: 31415.93" 



NSNumber *number = @1234; 

NSDictionary *dictionary = @{@"date": [NSDate date]}; 

NSString *baseString = @"Base string."; 

NSString *string2 = [baseString stringByAppendingFormat: 

     @" A number: %@, a dictionary: %@", number, dictionary]; 

// string2 is "Base string. A number: 1234, a dictionary: {date = 2005-10-17 09:02:01 -0700; }" 
+0

이 답변은 질문의 코드에서 자신의 방법과 변수를 사용하여 작성한 경우 OP가 더 도움이 될 것입니다. – rmaddy

0

당신이 문자열에 칼로리를 얻고있다하더라도, 당신은 추가하려면 다음 코드를 사용할 수 있습니다 및 레이블에 표시

NSString *calories1 = [_steakArray objectAtIndex:0]; 
NSString *calories2 = [_steakArray2 objectAtIndex:0]; 

int sum = [calories1 intValue] + [calories2 intValue]; 

self.infoLabel.text = [NSString stringWithFormat:@"%d",sum];