【问题标题】:How do I programmatically add a exit button to a UIView如何以编程方式将退出按钮添加到 UIView
【发布时间】:2012-02-16 14:16:34
【问题描述】:

当我在我的超级视图中创建一个 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];
}

【问题讨论】:

  • 在您可以呈现的第一个视图控制器中,它是导航控制器、UItabbar ..etc 吗?
  • 它是在第一个viewController中创建的,就像你屏幕上的一个弹出窗口,所以退出按钮会关闭这个窗口

标签: iphone objective-c xcode uiview


【解决方案1】:

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

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

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

在创建子视图时:

- (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!
}

【讨论】:

  • 我需要为那个按钮创建一个单独的方法吗?并在我触摸按钮时使用选择器调用该方法?
  • 是 - 如果您使用的是 InterfaceBuilder,则在视图的头文件中声明一个 - (IBAction) handleExit; 函数并连接 Touch Up Inside 带有视图的按钮事件。
  • 你介意告诉我如何以编程方式进行吗?因为在我的情况下,视图和按钮是以编程方式创建的
  • 当然 - 向我展示您创建 UIView 的代码(即子视图)以及添加按钮的位置。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是给你的代码:

            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];
    

    更改按钮放置的坐标

    myButtonTouchUpInside方法实现中,

    [self removeFromSuperview];
    

    希望这是你想要的..

    【讨论】:

    • 是的,这正是我正在寻找的,你介意告诉我如何实现 myButtonTouchUpInside。我试过这样的东西,但它不起作用'- (IBAction)exitView:(UIView *)sender { [sender removeFromSuperview]; }'
    • 发送者是按钮。您应该将removeFromSuperview 发送给自己,正如三个不同的人已经告诉您的那样。
    • 当我把 [self removeFromSuperview];在方法中,编译器说:“例如消息接收器类型'myClassName'没有声明带有选择器'removeFromSuperview'的方法。”那有什么问题? :(
    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多