Dismiss ModalView that was presented by tableViewC

2019-09-23 01:50发布

File Organization

In my project the app launches in ViewController, which loads inside a table (Table class) by adding it as a subview (ViewController.h - [self.view addSubview: Table.tableView];)

Camera Class is the detailed view, so when you tap in a table row it loads Camara.xib. Until here everything works fine.

The problem comes when I try to dismiss Camera.xib and go back to ViewController.xib, when I do that Camera.xib dismisses okay but instead of going back to ViewController.xib with Table.xib inside of it, the app only loads Table.xib, so the rest of objects that were placed in the view of ViewController.xib are not displaying.

What am I doing wrong?

Thanks in advance!!

CODE:

Table.m

- (void)viewDidLoad
{

    roomsArray = [[NSMutableArray alloc] initWithObjects:nil];
    [super viewDidLoad];

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // my code...

    Camara *camara = [[Camara alloc] initWithNibName:@"Camara" bundle:nil];
    [self presentViewController:camara animated:YES completion:nil];

}

Camara.m

-(IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

ViewController.m

- (void)viewDidLoad
{

    table = [[Table alloc] initWithNibName:@"Table" bundle:nil];

    [windows.tableView reloadData];
    [self.view addSubview:windows.tableView];
    windows.tableView.transform = CGAffineTransformMakeTranslation(0, 44);

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-23 02:21

Without seeing the code that you're using I cannot answer completely, but my guess is that you need to some something in viewWillAppear to reload your table or the table xib. viewWillAppear will get called every time your modal is dismissed.

The other option is to create a delegation protocol to allow your modal controller to call a method on your initial view controller.

This is a good article to start with: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-23 02:26

First of all I must state that your question has received downvotes, most likely because its written in poor english and initially it lacked lacks the necessary information required for a solution.

Next, I would like to stress that super calls such as [super viewDidLoad] should always come before any code logic. Move them to the top of each method.

Now as to why your having problems.

Whenever a view controller is created in memory it will execute its viewDidLoad method and then execute the viewWillAppear method just before it appears(on its parent unless your overload it), that execution only occurs once in the views lifetime. Any future attempts to display the view will result in a call to viewWillAppear.

Solution Move the following code

[windows.tableView reloadData];
[self.view addSubview:windows.tableView];
windows.tableView.transform = CGAffineTransformMakeTranslation(0, 44);

into

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [windows.tableView reloadData];
    [self.view addSubview:windows.tableView];
    windows.tableView.transform = CGAffineTransformMakeTranslation(0, 44);
}
查看更多
再贱就再见
4楼-- · 2019-09-23 02:31

the problem was that tableViewController and ViewController were in two view controllers, so I Combined them and now all works fine.

查看更多
登录 后发表回答