简体   繁体   English

试图关闭子视图和UIView

[英]Trying to dismiss subview and UIView

I'm still very new to iOS developing. 我对iOS开发还是很陌生。 In fact, if there is a super noob, I would be one :p. 实际上,如果有一个超级菜鸟,我将是一个:p。 Currently I am working on creating an IBAction button that accesses a subview. 目前,我正在创建一个访问子视图的IBAction按钮。 I have 2 ViewControllers, AddClientVC and NewClientVC , both with .nib files. 我有2个ViewController, AddClientVCNewClientVC ,都带有.nib文件。 So basically, inside my AddClientVC I implement an IBAction button with the following code: 因此,基本上,在我的AddClientVC内部,我使用以下代码实现了IBAction按钮:

- (IBAction)buttonPressed:(id)sender
{
    UIView *transparentBG = [[UIView alloc] initWithFrame:CGRectMake(-5, -5, 1500, 2500)];
    transparentBG.backgroundColor = [UIColor blackColor];
    transparentBG.opaque = NO;
    transparentBG.alpha = 0.5;
    [self.view addSubview:transparentBG];
    transparentBG.center = transparentBG.center;

    vc = [[NewClientVC alloc] initWithNibName:@"NewClientVC" bundle:nil];
    [self.view addSubview:vc.view];
    vc.view.center = self.view.center;
}

As you can see I implemented a UIView as a transparent background. 如您所见,我将UIView实现为透明背景。 Basically AddClientVC --> Transparent Background --> NewClientVC. 基本上是AddClientVC->透明背景-> NewClientVC。 Now I have created another IBAction button but this time inside NewClientVC as a function to dismiss the accessed subview which looks like this: 现在,我创建了另一个IBAction按钮,但这一次是在NewClientVC中,它是一个用来消除被访问的子视图的函数,如下所示:

- (IBAction)saveDismiss:(id)sender
{
    [self.view removeFromSuperview];
}

The problem I'm having right now is when I click the saveDismiss button it only removes the subview that I called previously on AddClientVC but it didn't remove the transparent background I have created as a UIView . 我现在遇到的问题是,当我单击saveDismiss按钮时,它仅删除了我先前在AddClientVC上调用的子视图,但没有删除我作为UIView创建的透明背景。 So the problem is how do I implement an action which simultaneously removes my subview and the UIView transparent background I created. 因此,问题在于如何实现同时删除我的子视图和创建的UIView透明背景的操作。

I need all the help I can get :) 我需要我能得到的所有帮助:)

Do not worry about code execution speed and stay confident in apple's SDK. 不必担心代码执行速度,并对Apple的SDK充满信心。 UIKit is optimized for best user experience. UIKit已针对最佳用户体验进行了优化。 Trying to boost your code by doing inappropriate SDK use is, in my opinion, a risky strategy. 我认为,尝试通过不适当使用SDK来增强代码是一种冒险的策略。 ;) – Vincent Zgueb ;)– Vincent Zgueb

Sorry Vincent but I don't agree with you. 对不起,文森特,但我不同意你的看法。 I reached here because I want to implement an gesture that adds a sub-view for my view, which will be the navigation of my app. 我到达这里是因为我想实现一个手势,为我的视图添加一个子视图,这将是我的应用程序的导航。

[self.view addSubview:ctrl.view]; 

is faster presenting the view than 呈现视图的速度比

[self.navigationController presentModalViewController:ctrl animated:NO]

and by the way, the solution to the topic in my case was: 顺便说一句,在我的案例中,该主​​题的解决方案是:

[self.view sendSubviewToBack:ctrl.view];

I'm not too sure I fully understand what you want to happen, but maybe you could try something like this? 我不太确定我是否完全了解您要发生的事情,但是也许您可以尝试这样的事情?

- (IBAction)saveDismiss:(id)sender
{
    [vc removeFromSuperView];
    [self.view removeFromSuperview];
}

I recommend not to manage your screens by adding subviews manually but instead use 我建议不要通过手动添加子视图来管理屏幕,而应使用

- (void)presentModalViewController: (UIViewController *)modalViewController
                          animated: (BOOL)animated

method on your root viewController. 根viewController上的方法。 Or better instantiate a UINavigationController and use push and pop methods to drill down/up your views. 或更好地实例化UINavigationController并使用push和pop方法来向下/向上浏览视图。 See apple reference here 在这里查看苹果参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM