2014-10-15 2 views
0

단추로 UIViewController를 만들고 싶습니다. 사용자가 특정 순서로 4 개의 버튼을 누르길 원합니다. 내가 지금까지했던 것은 내가 다음 코드로 모든 버튼에 임의의 태그 번호를 제공한다는 것입니다 :단추가 iOS에서 특정 순서로 누름

그래서
-(void)prepareArray { 
self.arary = [[NSMutableArray alloc]init]; 
while (self.arary.count <4) { 
    int value = arc4random()%4; 
    BOOL isFound = [[self.arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count]; 
    if(!isFound) 
     [self.arary addObject:[NSNumber numberWithInteger:value]]; 
} 
NSLog(@"%@", arary); 
button1.tag = [[arary objectAtIndex:0]intValue]; 
button2.tag = [[arary objectAtIndex:1]intValue]; 
button3.tag = [[arary objectAtIndex:2]intValue]; 
button4.tag = [[arary objectAtIndex:3]intValue]; 

내 질문은 : 나는 버튼을 누르는 순서를 가지고하는 방법을 만드는 방법 0에서 3까지? 나는 case와 if 문을 써서 시도했는데 이것을 알아낼 수 없다?

+0

모든 버튼에서 정수 값을 설정해야 시퀀스 순서를 알 수 있습니다. 버튼 코드를 보여 주시면 답변을 드릴 수 있습니다. – NullData

+0

코드는 모든 버튼에 정수 값을 설정하는 코드입니다 . 컨트롤러가 시작할 때마다 정수가 변경됩니다. 그게 내가 왜 추적하고 싶은지 0,1,2,3이 될 버튼을 누르라는 정확한 순서를 말해 보자. – axel

+0

버튼 시퀀스를 얻기위한 조건을두기가 어려워요? – NullData

답변

0
-(IBAction)buttonPressed:(UIButton *)button{ 

[clickDictionary setObject:[NSString stringWithFormat:@"%d",button.tag] forKey:button.titleLabel.text]; 
NSLog(@"clickDictionary : %@", clickDictionary); 

}

당신은 위와 같이 방법을 쓸 수 있습니다. 이 메서드는 모든 버튼을 클릭 할 때마다 호출됩니다. clickDictionary에서 버튼 이름과 태그를 가져옵니다. 필요에 따라 사전을 정렬 할 수 있습니다.

0

다음과 같은 방법으로 문제를 해결합니다.

for (int count = 0; count < 4; count++) 
{ 
    self.theButtons = [UIButton buttonWithType:UIButtonTypeSystem]; 
    self.theButtons = [[UIButton alloc] initWithFrame:CGRectMake(100, y, 100, 40)]; 
    [self.theButtons setTitle:[NSString stringWithFormat:@"Press Me"] forState:UIControlStateNormal]; 
    [self.theButtons setTag:[[data.randomArray objectAtIndex:count]intValue]]; 
    [self.theButtons addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.theButtons setBackgroundColor:[UIColor purpleColor]]; 
    [self.buttonArray addObject:theButtons]; 
    [self.view addSubview:theButtons]; 
    y=y+60; 
} 

과 함께 버튼에 태그를 부착 : 첫째로 나는 for 루프 내 버튼을 만들어 사용자가 내가 다른 생성 버튼을 누르면 주문하는 확인하려는 경우 마지막으로

self.randomArray = [[NSMutableArray alloc]init]; 
while (self.randomArray.count <4) { 
    int value = arc4random()%4+1; 
    BOOL isFound = [[self.randomArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count]; 
    if(!isFound) 
     [self.randomArray addObject:[NSNumber numberWithInteger:value]]; 
} 

배열 :

- (void)buttonClicked:(UIButton*)button 
    { 
     NSLog(@"Button %ld clicked.", (long int)[button tag]); 
     int value = [button tag]; 
     button.hidden = YES; 
     [buttonArray removeObject:button]; 
     //When user taps a button another array created which stores the button tags 
     if([addIntArray indexOfObject:[NSNumber numberWithInt:value]] == NSNotFound) { 
      [addIntArray addObject:[NSNumber numberWithInt:value]]; 
    } 

정적 배열로 버튼 클릭을 비교하십시오.

관련 문제