2013-06-13 1 views
0

버튼을 누를 때마다 ABC와는 다른 문자를 표시하고 싶습니다.각 언론에 대한 UIButton 제목 바꾸기

그래서 응용 프로그램을 처음 클릭하면 사용자는 문자 A를 볼 수 있습니다. 버튼을 누르면 문자 B가 표시됩니다.

나의 도전 과제는 버튼을 클릭 할 때마다 제목 텍스트를 바꾸는 방법입니다. 2 개의 함수 중 일부 코드를 적어 두었습니다. 더 자세히 살펴보면이 매번 호출되기 때문에 배열의 첫 번째 멤버를 얻을 때마다 처음 문자 A를 볼 수 있습니다. 버튼을 누르면 배열의 첫 번째 멤버 인 문자 B가 표시되지만 매번 plist 파일을로드 할 때마다 동일한 문자가 표시되므로 배열의 첫 번째 멤버가 계속 호출되므로 .

어떻게 해결할 수 있습니까?

-(void)createLoginBioButton 
{ 
    authButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [authButton setBounds:CGRectMake(300,300, 150, 150)]; 
    [authButton setCenter:CGPointMake(150, 240)]; 
    [self.view addSubview:authButton]; 
    [authButton setEnabled:true]; 
    [authButton setTitle:@"A" forState:UIControlStateNormal]; 
    [authButton setFont:[UIFont systemFontOfSize:70]]; 
    [authButton addTarget:self 
        action:@selector(displayABC:) 
     forControlEvents:UIControlEventAllTouchEvents]; 
} 

-(void)displayABC:(id)sender 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"plist"]; 
    NSArray *ABCArray = [NSArray arrayWithContentsOfFile:path]; 
    for (NSString *ABCValues in ABCArray){ 
     [authButton setTitle:ABCValues forState:UIControlStateNormal]; 
    } 
} 

답변

0

현재 문자 색인에 대한 참조를 인스턴스 변수로 유지하고 각 문자를 하나씩 늘리면 어떨까요?

사이비 코드

@property NSArray *abcs; 
@property int currentIndex; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.abcs = //load it from the plist 
    [self updateButtonText]; 
} 

- (void)buttonTouched 
{ 
    self.currentIndex++; 

    if (self.currentIndex >= self.abcs.count) { 
    self.currentIndex = 0; 
    } 
    [self updateButtonText]; 
} 
- (void)updateButtonText 
{ 
    [self.button setTitle:self.abcs[self.currentIndex] forControlState:UIControlStateNormal]; 
} 
+0

인덱스를 추적 유지하는 정적 변수를 넣어하는 것입니다. 'sudo'는 유닉스 명령이다. – tooluser

+1

하하 좋은 캐치 – eunoia

0

간단한 솔루션은 내가 그 '의사 코드'생각

-(void)createLoginBioButton 
    { 

    authButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [authButton setBounds:CGRectMake(300,300, 150, 150)]; 
    [authButton setCenter:CGPointMake(150, 240)]; 
    [self.view addSubview:authButton]; 
    [authButton setEnabled:true]; 
    [authButton setTitle:@"A" forState:UIControlStateNormal]; 
    [authButton setFont:[UIFont systemFontOfSize:70]]; 
    [authButton addTarget:self action:@selector(displayABC:) forControlEvents:UIControlEventAllTouchEvents]; 
} 

-(void)displayABC:(id)sender 
{ 

    static int index = 0; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"plist"]; 
    NSArray *ABCArray = [NSArray arrayWithContentsOfFile:path]; 
    if(index < [ABCArray count]) 
     [authButton setTitle:[ABCArray objectAtIndex:index] forState:UIControlStateNormal]; 
    index++; 


} 
+0

Mihir 나는 똑같은 것을 얻고있다. 그것은 편지 A를 선물 한 다음 편지를 보낸다. – user2444326

+0

색인이 증가하고 계십니까? 또는 그렇지 않습니다 ... 위 코드를 사용해 보셨습니까? –

관련 문제