Objective C iOSアプリケーションで、配列内のオブジェクトの量に応じて複数のボタンを作成しているところに問題があるようです。私はスウィフトを流暢に知っているので、私はロジックをスウィフトに複製しました。しかし、Objective Cでは、(forループを削除した後で)ボタンのテキストを見ることができないか、複数のボタンを作成することができません。たとえば、3つの名前からなる配列があります。私は対応する名前に設定されたタイトルで各名前のボタンを作成したいと思います。これまでのところ、私はこれを持っています: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)年齢配列を設定するときにバインドされたループのためである:私はこれが正常に機能を停止する4つの問題は、(他の人があるかもしれません)が見つかり
おかげで、
Arnav K.
[button setTitle:[age objectAtIndex:x]]; –
'forControlEvents:'を追加する必要はありませんか? –