简体   繁体   中英

Trying to dismiss subview and UIView

I'm still very new to iOS developing. In fact, if there is a super noob, I would be one :p. Currently I am working on creating an IBAction button that accesses a subview. I have 2 ViewControllers, AddClientVC and NewClientVC , both with .nib files. So basically, inside my AddClientVC I implement an IBAction button with the following code:

- (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. Basically AddClientVC --> Transparent Background --> 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)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 . So the problem is how do I implement an action which simultaneously removes my subview and the UIView transparent background I created.

I need all the help I can get :)

Do not worry about code execution speed and stay confident in apple's SDK. UIKit is optimized for best user experience. Trying to boost your code by doing inappropriate SDK use is, in my opinion, a risky strategy. ;) – 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. Or better instantiate a UINavigationController and use push and pop methods to drill down/up your views. See apple reference here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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