在运行时,在objective-c中,如何将UIObject
放入UIView
?
谢谢。
答案 0 :(得分:3)
只要视图和对象都存在,您就可以随时在UIView中放置UIObject(如果不存在,则可以创建它们)。如果对象是UIView的子类(我假设它是),那么只需执行:
[myView addSubview:myObject];
只要您已创建myView
和myObject
(例如alloc
和init
),就可以将此代码放在任意位置。
答案 1 :(得分:2)
您可以执行以下操作 -
UIImage *closeButtonImage = [UIImage imageNamed:@"closeButton.png"];
[closeButton setImage: closeButtonImage forState:UIControlStateNormal];
[self addSubview:closeButton];
答案 2 :(得分:1)
当主UIView加载时,它会调用viewDidLoad
& viewDidAppear
。你需要超越这些方法和把你的对象创建在这些方法中。
假设您想在运行时创建UILabel
。您将以下代码放在viewDidLoad
&你的对象被创建了&作为子视图添加到主视图中。
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width-150,0,50,15)];
[newLabel setText:@"Method: "];
newLabel.textAlignment = UITextAlignmentLeft;
newLabel.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
newLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:11];
[self.view addSubview:newLabel];
[newLabel release];
您无需仅在这些方法中添加(或创建)UI对象。如果您已定义任何手势&为他们指定了选择器方法,你可以创建&在这些方法中添加对象......
答案 3 :(得分:1)
分配这样的UI,例如
UIButton* btn = [[UIButton alloc] init];
[btn setFrame:desiredFrame];
[view addSubview:btn];
答案 4 :(得分:0)
让你的对象成为一个Button,然后在你的方法中使用以下代码(你要将对象添加到UIView
):
UIButton *objectButton = [[UIButton alloc] init];
[objectButton setImage:actualImage forState:UIControlStateNormal];
[objectButton setTag:intValue];
[objectButton addTarget:self action:@selector(anyMethod:)forControlEvents:UIControlEventTouchDown];
[self addSubview:iconImageSlide];
此处[self addSubview:iconImageSlide];
将对象添加到您的视图中。