0

如何UIview用两个自定义一个UIbutton用户可以将参数传递给自定义类的地方

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    CanvasView *newCanvas = [[CanvasView alloc] initWithFrame:CGRectMake(0.0, 164.0, self.view.frame.size.width, self.view.frame.size.height-350)];
    self.canvas = newCanvas;
    [self.view addSubview:canvas];

}

我只能自定义UIView!如何将两者添加UIButton到视图中。

当我分配自定义类时,它也UIView需要可见UIButtons

4

1 回答 1

1

您可以使用以下代码创建动态按钮

CanvasView *newCanvas = [[CanvasView alloc] initWithFrame:CGRectMake(0.0, 164.0, self.view.frame.size.width, self.view.frame.size.height-350)];
self.canvas = newCanvas;
[self.view addSubview:newCanvas];


UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:@"Ok" forState:UIControlStateNormal];
[but addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[newCanvas addSubview:but];

您可以使用 insertSubview:atIndex 方法

 [newCanvas insertSubview:myButton atIndex:0];

希望它可以帮助你。

于 2012-06-18T12:30:36.247 回答