배열에있는 객체의 양에 따라 여러 버튼을 만드는 내 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.
[button setTitle : [age objectAtIndex : x]]; –
'forControlEvents :'를 추가 할 필요가 없습니까? –