Ich habe ein Problem in meiner Objective-C-iOS-App, wo ich je nach Anzahl der Objekte in einem Array mehrere Schaltflächen erzeuge. Ich kenne Swift fließend, also habe ich die Logik in Swift nachgebildet, und es hat funktioniert. In Objective C kann ich jedoch den Text der Schaltfläche nicht sehen (nachdem ich die for-Schleife entfernt habe) oder mehrere Schaltflächen erstellen. Zum Beispiel habe ich ein Array voller drei Namen. Ich möchte für jeden Namen eine Schaltfläche mit dem Titel auf den entsprechenden Namen erstellen. Bisher habe ich folgendes:UIButton setzt keinen Text
#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
Wenn jemand etwas falsch mit diesem Code sieht, bitte weisen Sie darauf hin!
Danke,
Arnav K.
[Schaltfläche setTitle: [Alter objectAtIndex: x]]; –
Sie müssen kein 'forControlEvents:' hinzufügen? –