2017-12-30 19 views
0

배열에있는 객체의 양에 따라 여러 버튼을 만드는 내 Objective C iOS 앱에 문제가있는 것 같습니다. 스위프트는 유창하게 잘 알고 있으므로 로직을 스위프트에 복제했습니다. 그러나 Objective C에서는 for 루프를 제거한 후에 버튼 텍스트를 볼 수 없거나 여러 버튼을 만들 수 없습니다. 예를 들어 배열에 세 개의 이름이 있습니다. 해당 이름으로 설정된 제목으로 각 이름에 대한 단추를 만들고 싶습니다. 지금까지이 코드를 가지고 있습니다 :UIButton이 텍스트를 설정하지 않습니다.

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


NSMutableArray *ages = [[NSMutableArray alloc] init]; 

for (int i = 0; i > 10; i++) { 
    [ages addObject:[NSString stringWithFormat:@"%i", i]]; 
} 


UIScrollView *scrollView= [[UIScrollView alloc]initWithFrame:self.view.frame]; 
scrollView.delegate= self; 
self.automaticallyAdjustsScrollViewInsets= NO; 
scrollView.backgroundColor= [UIColor clearColor]; 
scrollView.scrollEnabled= YES; 
scrollView.userInteractionEnabled= YES; 
[scrollView setShowsHorizontalScrollIndicator:NO]; 
[scrollView setShowsVerticalScrollIndicator:NO]; 


CGFloat xValue = 0; 
for(int x=0; x > ages.count; x++){ 
    UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(xValue ,0 , 172 ,65)]; 
    UIColor *buttonOutline = [[UIColor redColor] CGColor]; 
    button.layer.borderColor = [buttonOutline CGColor]; 
    button.layer.backgroundColor = [[UIColor clearColor] CGColor]; 
    button.layer.borderWidth = 1.0; 
    button.layer.cornerRadius = 6.0; 
    [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]]; 
    button.titleLabel.text = [ages objectAtIndex:x]; 
    [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside]; 
    [scrollView addSubview:button]; 
    NSLog(@"Button Added"); 
    xValue = button.frame.size.width + 40; 

} 

scrollView.contentSize = CGSizeMake(xValue, 65); 
[self.view addSubview:scrollView]; 

} 

- (void)test:(UIButton*)sender{ 
    NSLog(@"Clicked %@", sender.titleLabel.text); 
} 



@end 

이 코드에 아무런 문제가 없다면, 지적 해주십시오!

1) 설정할 때 바인딩 루프 세의 배열입니다 :

감사합니다,

Arnav K.

+1

[button setTitle : [age objectAtIndex : x]]; –

+0

'forControlEvents :'를 추가 할 필요가 없습니까? –

답변

1

나는 네 문제는 (다른 사람이있을 수있다)을 제대로 작동을 멈출 것을 발견 부정확하다. 2) 단추를 만들 때 for 루프 바운드가 틀립니다. 3) buttonOutline 색상을 잘못 설정했습니다. 4) xValue가 잘못 업데이트되고 있습니다.

다음은 변경이 된 후의 viewDidLoad 방법입니다 : 원래에 비교할 수 있도록

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSMutableArray *ages = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < 10; i++) { // UPDATED 
     [ages addObject:[NSString stringWithFormat:@"%i", i]]; 
    } 


    UIScrollView *scrollView= [[UIScrollView alloc]initWithFrame:self.view.frame]; 
    scrollView.delegate= self; 
    self.automaticallyAdjustsScrollViewInsets= NO; 
    scrollView.backgroundColor= [UIColor clearColor]; 
    scrollView.scrollEnabled= YES; 
    scrollView.userInteractionEnabled= YES; 
    [scrollView setShowsHorizontalScrollIndicator:NO]; 
    [scrollView setShowsVerticalScrollIndicator:NO]; 


    CGFloat xValue = 0; 
    for(int x=0; x < ages.count; x++){ // UPDATED 
     UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(xValue ,0 , 172 ,65)]; 
     UIColor *buttonOutline = [UIColor redColor]; // UPDATED 
     button.layer.borderColor = [buttonOutline CGColor]; 
     button.layer.backgroundColor = [[UIColor clearColor] CGColor]; 
     button.layer.borderWidth = 1.0; 
     button.layer.cornerRadius = 6.0; 
     [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]]; 
     button.titleLabel.text = [ages objectAtIndex:x]; 
     [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside]; 
     [scrollView addSubview:button]; 
     NSLog(@"Button Added"); 
     xValue += button.frame.size.width + 40; // UPDATED 

    } 

    scrollView.contentSize = CGSizeMake(xValue, 65); 
    [self.view addSubview:scrollView]; 
} 

나는 갱신 된 코멘트와 함께 변화 네 줄을 표시했다.

편집 텍스트와 텍스트 색상은 다음과 같은 사용 변경하려면

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
[button setTitle:[ages objectAtIndex:x] forState:UIControlStateNormal]; 

이 제거 :

button.titleLabel.text = [ages objectAtIndex:x]; 

당신은 당신이 다른 텍스트를 설정할 수 있기 때문에이 작업을 수행 할 필요가 단추의 다른 상태는 모두 자동으로 처리됩니다.

+0

이 프로그램을 실행 해 보았지만 제목이 설정되지 않았습니다. 심지어 글꼴 색을 추가했지만 텍스트가 나타나지 않습니다. 제목 색의 코드는'[button setTitleColor : buttonOutline forState : UIControlStateSelected];'입니다. 왜 이런 일이 일어나고 있는지 아십니까? –

+0

죄송합니다, '[button setTitleColor : buttonOutline forState : UIControlStateNormal];' –

+1

죄송합니다, 나는 그 밖의 모든 것을 일하고있어 원래의 문제를 잊어 버렸습니다. 나는 대답을 편집 할 것이다. –

관련 문제