0

たとえば、ユーザーがボタンに触れたときに要素(UIViewsなど)を作成できるようにしたい

NSMutableString *myVar = [NSMutableString stringWithFormat:@"_view%i", num];
UIView * newView = [self valueForKey:myVar];

しかし、すべてを追加することなく

UIView * _view1;
UIView * _view2;
...

.h ファイル内 (これが可能な場合のみ..)

4

4 に答える 4

1

これは、あなたが望むことをするサンプルコードです。

@interface MyViewController ()

@property (strong, nonatomic) NSMutableArray *listChildViews;

@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.listChildViews = [[NSMutableArray alloc] init];
}

- (IBAction)addChildViewTapped:(id)sender
{
    int numChildViews = [self.listChildViews count];

    ++numChildViews;

    // add new child view
    NSString *labelForNewView = [NSString stringWithFormat:@"view %d", numChildViews];

    CGFloat labelHeight = 28.0;

    UILabel *childView = [[UILabel alloc] initWithFrame:CGRectMake(10, numChildViews*labelHeight, 120.0, labelHeight)];
    childView.text = labelForNewView;
    [self.listChildViews addObject:childView];
    [self.view addSubview:childView];
}

@end
于 2013-07-24T14:53:43.337 に答える
0

.hビューをファイルに追加する必要はありません。それらを追加する前と場所でインスタンス化するだけです

-(void) addButton
{
UIView *view = [self view];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"My Button" forState:UIControlStateNormal];

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
[myLabel setText:@"My Label"];

[button1 sizeToFit];
[myLabel sizeToFit];

[view addSubview:button1];
[view addSubview:myLabel];
}
于 2013-07-24T15:13:01.747 に答える