use*_*213 2 objective-c uibutton uiview ios
我如何解雇超过主要内容的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];
}];
}
Run Code Online (Sandbox Code Playgroud)
您正尝试删除本地创建的视图,但稍后当您尝试从超级视图中删除它时,您没有引用它.
这就是你所做的,你创建并初始化了一个新的UIView settingsPopover
UIView *settingsPopover = [[UIView alloc] init];
...
[self.view addSubview:settingsPopover];
Run Code Online (Sandbox Code Playgroud)
然后,您将删除另一个名称_settingsPopover,该视图与showUserSettings方法中的本地创建的视图无关.
我假设你已经创建了一个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];
Run Code Online (Sandbox Code Playgroud)
这样settingsPopover,您在showUserSettings方法中本地创建的新创建的类型视图变量将被复制到名为_settingsPopovervariable 的全局变量,这就是您要添加为子视图的变量.
这样,您可以参考它以后,当你想从你已与做的SuperView删除它[_settingsPopover removeFromSuperview];在你的closeUserSettings方法.你所有的其他代码都没问题.
| 归档时间: |
|
| 查看次数: |
399 次 |
| 最近记录: |