xcode 如何以编程方式向 UIView 添加退出按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8968115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:04:02  来源:igfitidea点击:

How do I programmatically add a exit button to a UIView

iphoneobjective-cxcodeuiview

提问by Shakesbeer

When I created a UIView in my superview, I want to add a button to this view that act as an exit button. So when pressed, it will remove this UIView from my superview, how should I do that? The View and button in that view is created programmatically when a button is pressed

当我在我的超级视图中创建一个 UIView 时,我想向这个视图添加一个按钮作为退出按钮。所以当按下时,它会从我的超级视图中删除这个 UIView,我应该怎么做?该视图中的视图和按钮是在按下按钮时以编程方式创建的

- (IBAction)skillButtonPressed:(UIButton *)sender 
{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
    [cancelButton setTitle:@"x" forState:UIControlStateNormal];

    [myView addSubview:cancelButton];
    [self.view addSubview:myView];
}

回答by Peter Sarnowski

In your class that creates the view, add a function like this:

在创建视图的类中,添加如下函数:

//tag for subview - make it unique
#define SUBVIEW_TAG 9999 

- (void) handleExit
{
    UIView * subview = [self.view viewWithTag:SUBVIEW_TAG];
    [subview removeFromSuperview];
}

and when creating the subview do:

创建子视图时,请执行以下操作:

- (IBAction)skillButtonPressed:(UIButton *)sender 
{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cancelButton addTarget:self action:@selector(handleExit) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
    [cancelButton setTitle:@"x" forState:UIControlStateNormal];

    [myView addSubview:cancelButton];
    [myView setTag:SUBVIEW_TAG];
    [self.view addSubview:myView];

    //don't forget to release if you are not using ARC!
}

回答by Mrunal

Here is the code for you:

这是给你的代码:

        self->myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self-> myButton = CGRectMake(60, 6, 180, 30);
    [self-> myButton setTitle:@"MyButton" forState:UIControlStateNormal];
    [self-> myButton setTextColor:[UIColor whiteColor]];
    self-> myButton = 1;
    [self-> myButton addTarget:self action:@selector(myButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self-> myButton];

Change the coordinates for your button placing

更改按钮放置的坐标

In myButtonTouchUpInsidemethod implemetation,

myButtonTouchUpInside方法实现中,

[self removeFromSuperview];

Hope this is what you want..

希望这是你想要的..

回答by Abhinandan Sahgal

You can even hide the view or [self removeFromSuperview];

您甚至可以隐藏视图或 [self removeFromSuperview];