Lyd
Lyd

Reputation: 2096

How to remove view in Objective C

I have a View (CupsViewController) with a Label and when it's clicked, TableView (AddressTableController) slides from the bottom of the screen and stays in the lower half. In TableView, when Ok Buttonis pressed, I want to change value of Labeland remove TableViewwith animation (that is, slide to the bottom).

Here is my code:

CupsViewController

Method called when click on Label

- (IBAction)showPop:(id)sender
{
    addressTableController = [[AddressTableController alloc]initWithStyle:UITableViewStyleGrouped];
    [addressTableController setDelegate:self];
    [[self view] addSubview:[addressTableController myTableView]];
    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [[addressTableController myTableView] setFrame:CGRectMake(0, kScreenHeight * 0.5, kScreenWidth, kScreenHeight * 0.5)];
    } completion:nil];
}

Method called from AddressTableControllerwhen Buttonis pressed

- (void)addressTableController:(AddressTableController *)viewController didChooseValue:(int)value {
    direccionLabel.text = [[[[myAppDelegate usuarioActual] cups] objectAtIndex:value] direccion];
    [addressTableController.view removeFromSuperview];
}

Image

enter image description here

As you can see, I've tried with removeFromSuperviewbut it does nothing. How can I slide TableViewto the bottom?

Upvotes: 0

Views: 2818

Answers (1)

ivanmoskalev
ivanmoskalev

Reputation: 2044

It seems that you myTableView property returns a different view, not the one that is referenced in the view property. So you basically add the former as a subview ([[self view] addSubview:[addressTableController myTableView]];), but try to remove the latter ([addressTableController.view removeFromSuperview];).

Just do

[[addressTableController myTableView] removeFromSuperview];

instead of

[addressTableController.view removeFromSuperview];

Here you go. Cheers, mate! :)


P.S. if you want to animate the view out, you can do it like this

[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    // animation code...
} completion:^(BOOL finished) {
    [[addressTableController myTableView] removeFromSuperview];
}];

Upvotes: 1

Related Questions