在运行时,在objective-c中,如何将UIObject放入UIView中?

时间:2021-03-05 19:58:04

At run-time, in objective-c, how to place a UIObject into a UIView?

在运行时,在objective-c中,如何将UIObject放入UIView中?

Thank you.

5 个解决方案

#1


3  

You can place a UIObject in a UIView whenever you like so long as the view and object both exist (and you can create them if they don't). If the object is a subclass of UIView (which I assume it is), then just do:

只要视图和对象都存在,您就可以随时将UIObject放在UIView中(如果不存在,则可以创建它们)。如果对象是UIView的子类(我假设它是),那么就这样做:

[myView addSubview:myObject];

Place this code wherever you like, so long as both myView and myObject have already been created (with alloc and init, for example).

将此代码放在任何您喜欢的位置,只要myView和myObject都已创建(例如,使用alloc和init)。

#2


2  

You can do something like the following --

您可以执行以下操作 -

UIImage *closeButtonImage = [UIImage imageNamed:@"closeButton.png"];
[closeButton setImage: closeButtonImage forState:UIControlStateNormal];
[self addSubview:closeButton];

#3


1  

When the main UIView loads it invokes viewDidLoad & viewDidAppear. You need to over-ride these methods & put your object creation in these methods.

当主UIView加载时,它会调用viewDidLoad和viewDidAppear。您需要覆盖这些方法并将对象创建放在这些方法中。

Lets say you want to create a UILabel at runtime. You place the following code in viewDidLoad & your object gets created & added as a subview to the main view.

假设您想在运行时创建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];

You need not add (or create) UI objects only in these methods. If you have defined any gestures & have specified selector methods for them, you could create & add objects in those methods too...

您无需仅在这些方法中添加(或创建)UI对象。如果您已经定义了任何手势并为它们指定了选择器方法,那么您也可以在这些方法中创建和添加对象......

#4


1  

Allocate the UI like this for example

例如,像这样分配UI

UIButton* btn = [[UIButton alloc] init];
[btn  setFrame:desiredFrame];
[view addSubview:btn];

#5


0  

let your object be a Button, then use following code in your method(where you want to add object to UIView):

让你的对象成为一个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];

here [self addSubview:iconImageSlide]; add object to your view.

这里[self addSubview:iconImageSlide];将对象添加到视图中。

#1


3  

You can place a UIObject in a UIView whenever you like so long as the view and object both exist (and you can create them if they don't). If the object is a subclass of UIView (which I assume it is), then just do:

只要视图和对象都存在,您就可以随时将UIObject放在UIView中(如果不存在,则可以创建它们)。如果对象是UIView的子类(我假设它是),那么就这样做:

[myView addSubview:myObject];

Place this code wherever you like, so long as both myView and myObject have already been created (with alloc and init, for example).

将此代码放在任何您喜欢的位置,只要myView和myObject都已创建(例如,使用alloc和init)。

#2


2  

You can do something like the following --

您可以执行以下操作 -

UIImage *closeButtonImage = [UIImage imageNamed:@"closeButton.png"];
[closeButton setImage: closeButtonImage forState:UIControlStateNormal];
[self addSubview:closeButton];

#3


1  

When the main UIView loads it invokes viewDidLoad & viewDidAppear. You need to over-ride these methods & put your object creation in these methods.

当主UIView加载时,它会调用viewDidLoad和viewDidAppear。您需要覆盖这些方法并将对象创建放在这些方法中。

Lets say you want to create a UILabel at runtime. You place the following code in viewDidLoad & your object gets created & added as a subview to the main view.

假设您想在运行时创建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];

You need not add (or create) UI objects only in these methods. If you have defined any gestures & have specified selector methods for them, you could create & add objects in those methods too...

您无需仅在这些方法中添加(或创建)UI对象。如果您已经定义了任何手势并为它们指定了选择器方法,那么您也可以在这些方法中创建和添加对象......

#4


1  

Allocate the UI like this for example

例如,像这样分配UI

UIButton* btn = [[UIButton alloc] init];
[btn  setFrame:desiredFrame];
[view addSubview:btn];

#5


0  

let your object be a Button, then use following code in your method(where you want to add object to UIView):

让你的对象成为一个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];

here [self addSubview:iconImageSlide]; add object to your view.

这里[self addSubview:iconImageSlide];将对象添加到视图中。