2013-07-05 5 views
-1

나는 이것을 위해 상당히 광범위하게 행운을 찾았으며, 나는 그게 사소한 것이라고 확신합니다.단추를 사용하여 NSArray를 변경하십시오.

기본적으로 수정 구슬 유형의 앱입니다. 내가 가진 :

self.predictionArray = [[NSArray alloc] initWithObjects: 
         @"Example 1",  
         @"example 10000", nil]; 

내가 탭에 "magic8"버튼을, 선택 :

- (IBAction)buttonPressed:(UIButton *)sender { 
NSUInteger index = arc4random_uniform(self.predictionArray.count); 
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];} 

이 모두 완벽하게 작동하고, 물건을 많이 그것에 대해 웹에있다.

내가하고 싶은 것 :

이 배열의 3, 3 개 버튼이. 버튼은 단순히 해당 NSArray를 선택한 다음 "magic8"버튼을 누릅니다.

상당히 단순한 것처럼 보입니다. 현재 코드에서 무슨 일이 벌어지고 있는지 알지만, 내 인생에서 어떻게 움직이는 지 알 수 없습니다. 나는 확실히 경험이 없다.

조언을 제공 할 수있는 사람에게 많은 감사를드립니다. 나는 정말로 누군가의 시간을 낭비하고 싶지 않다. 나는 운을 찾지 못했고 아마도 지식을 가진 사람이 1 분 정도 걸릴 것이라고 생각했다.

+0

앱을 작성하기 전에 프로그래밍 기본 사항 (실제 도서 또는 수업 과정 포함)을 배우는 데 시간을 할애하는 것이 좋습니다. –

답변

0

당신은 정말 3 버튼의 일을 수행하려면 다음

- (IBAction) buttonAction:(UIButton *) b 
{ 
switch(b.tag) 
{ 
case 1: 
{ 
self.predictionArray = nil; 
self.predictionArray = [[NSArray alloc] initWithObjects: 
        @"Example 1",  
        @"example 10000", nil]; 
} 
break; 

case 2: 
{ 
self.predictionArray = nil; 
self.predictionArray = [[NSArray alloc] initWithObjects: 
        @"Example 2",  
        @"example 20000", nil]; 
} 

break; 
case 3: 
{ 
self.predictionArray = nil; 
self.predictionArray = [[NSArray alloc] initWithObjects: 
        @"Example 3",  
        @"example 30000", nil]; 
} 

break; 
default: 
break; 
} 

- (IBAction)buttonPressed:(UIButton *)sender { 
NSUInteger index = arc4random_uniform(self.predictionArray.count); 
self.predictionLabel.text = [self.predictionArray objectAtIndex:index]; 
} 
+0

아, 그리고 button2를 클릭하면 "predictionArray"버전이로드됩니까? – Willl

+0

예, 그게 콘텐츠입니다. 1 개의 메소드를 사용할 수도 있고 3 개의 버튼 모두 호출 할 수 있으며, 버튼 1,2,3의 태그를 설정하면 태그를 전환하고 메소드의 배열 내용을 변경하면됩니다. – soryngod

+0

감사합니다. 이것은 완벽하게 작동했습니다. 나는 그것이 매우 깨끗하고 좋은 방법은 아니라고 생각하지만 훌륭하게 작동합니다. – Willl

1

을 간단한 (하지만 가장 좋은) 솔루션은 0, 1, 2. 그런 다음 태그 인터페이스 빌더에 3 개 버튼을 제공하는 것입니다 설정하여 predictionArray에 :

- (IBAction)buttonPressed:(UIButton *)sender { 
    if (sender.tag >= 0 && sender.tag < self.predictionArray.count) { 
     NSArray *predictionArray = self.predictionArray[sender.tag]; 
     NSUInteger index = arc4random_uniform(predictionArray.count); 
     self.predictionLabel.text = [predictionArray objectAtIndex:index]; 
    } 
} 
,369 :
@[ @[ @"button 0 123", @"button 0 456" ], 
    @[ @"button 1 123", @"button 1 456" ], 
    @[ @"button 2 123", @"button 2 456" ]] 

는 그런 다음 buttonPressed 방법 당신은 당신의 버튼 태그에 따라 부분 배열을 선택할 것
+1

간단하지만 적절합니다. Willl에 대해 명확히하기 : 세 개의 모든 버튼의 동작은이 단일 'buttonPressed :'핸들러에 연결됩니다. – herzbube

관련 문제