gpt4 book ai didi

ios - UIButton 关闭 UIView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:32 25 4
gpt4 key购买 nike

如何关闭覆盖主要内容的 UIView?将其视为带有关闭按钮的游览弹出窗口。这是我正在使用的代码,但它不起作用。没有错误或警告,似乎并没有解决问题。有任何想法吗?

    - (void) showUserSettings {
UIView *settingsPopover = [[UIView alloc] init];
settingsPopover.frame = CGRectMake(20, 600, 280, 350);
[settingsPopover.layer setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(241/255.0) blue:(241/255.0) alpha:1].CGColor];

[UIView animateWithDuration:0.35 animations:^{
settingsPopover.frame = CGRectMake(20, 130, 280, 350);
settingsPopover.alpha = 1.0f;
} completion:^(BOOL finished) {
}];


UIButton *closeSettings = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 35, 40)];
[closeSettings setBackgroundColor:[UIColor colorWithRed:(212/255.0) green:(121/255.0) blue:(146/255.0) alpha:1]];
[closeSettings setTitle:@"X" forState:UIControlStateNormal];
[closeSettings addTarget:self action:@selector(closeSettings) forControlEvents:UIControlEventTouchUpInside];

[settingsPopover addSubview:closeSettings];
[self.view addSubview:settingsPopover];
}

- (void) closeSettings {
[UIView animateWithDuration:0.35 animations:^{
_settingsPopover.frame = CGRectMake(20, 400, 280, 350);
_settingsPopover.alpha = 1.0f;
} completion:^(BOOL finished) {
[_settingsPopover removeFromSuperview];
}];
}

最佳答案

您正在尝试删除一个在本地创建的 View ,但您稍后在尝试将其从父 View 中删除时没有引用它。

这就是您所做的,您创建并初始化了一个名为 settingsPopover 的新 UIView

UIView *settingsPopover = [[UIView alloc] init];    
...
[self.view addSubview:settingsPopover];

然后您要删除名为 _settingsPopover另一个 View 它与您在 showUserSettings 中本地创建的 View 没有关联方法。

我假设您已经创建了一个类型为 UIView 的全局变量称为 _settingsPopover .如果是这样的话,那么它应该在你的 -(void)showUserSettings 中。方法添加一行并更改另一行以修复它。看看下面:

-(void)showUserSettings{
...
[settingsPopover addSubview:closeSettings]; <-- in your method the code up to here is fine

// add this line before your addsubview code
_settingsPopover = settingsPopover; //We are copying over your local variable to your global variable

//and change this line from 'settingsPopover' to '_settingsPopover'
[self.view addSubview:_settingsPopover];

这样,您新创建的 View 类型变量称为 settingsPopover您在 showUserSettings 中本地创建的方法将被复制到名为 _settingsPopover 的全局变量中变量,这就是您要添加为 subview 的变量。

这样你可以稍后在你想从你已经在用 [_settingsPopover removeFromSuperview]; 做的 superView 中删除它时引用它在你的closeUserSettings方法。您的所有其他代码都很好。

关于ios - UIButton 关闭 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com