2017-12-30 55 views
0

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.

+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]; 
} 

私は、更新されたコメントで変更4行をマークしています。次を使用し、テキストとテキストの色を変更するには

EDIT

[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

申し訳ありませんが、私は他のすべての作業をして元の問題を忘れました。私は答えを編集します。 –

関連する問題