使用[self.view removeFromSuperview]

时间:2016-04-15 05:53:53

标签: ios objective-c iphone

我是iPhone编程的新手。

基本上我有一个MFSideMenuViewController,其设置viewController为rightSideViewController。

设置ViewController与UITableView一起使用,当我点击任何Index of tableview时,它将另一个VC加载为[rootViewController.view addSubView:VC];并使用实现的后退按钮代码 - [self.view removeFromSuperview];,直到现在它正常工作

SettingsVC.h
//UITableView Delegate method
if(indexPath.row==1){
VC=[[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"vcStoryBoardIdentifier"];
[VC.view setFrame:[[APP_DELEGATE window] frame]];
[[[[APP_DELEGATE window] rootViewController] view] addSubview:VC.view];
}

在这个VC我有一个tableview。当我点击任何index path.row时,它正在将另一个名为anotherVC的VC作为子视图加载到VC [VC.view addSubView:anotherVC];

    VC.m
    - (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view.

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTblPS) name:@"VCTblReload" object:nil];
        }

-(void)reloadTblPS{
   [VCTableView reloadData];
}

    //UITableview DataSource method
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                static NSString *CellIdentifier = @"Cell";
                    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    if(cell == nil)
                    {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                     if(indexPath.row == 0)
                        {
                button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setImage:img1 forState:UIControlStateNormal];
                [button setImage:img2 forState:UIControlStateHighlighted];
                [button setFrame:CGRectMake(220,10,60,20)];
                [button addTarget:self action:@selector(openAnotherVc:) forControlEvents:UIControlEventTouchUpInside];
                            [cell.contentView addSubview:button];
                }
                }
                return cell;

                }
    -(void)openAnotherVc:(id)sender{

            anotherVC = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"anotherVCStoryBoardID"];
                    [[anotherVC view] setFrame:[[APP_DELEGATE window] frame]];
                    [self.view addSubview:[anotherVC view]];

            }
    -(IBAction)VCcancelBtn:(id)sender{
                [self.view removeFromSuperview];
            //working fine
            }

这另一个VC还包含带有`[anotherVC.view removeFromSuperView]的取消按钮;

anotherVC.m
-(IBAction)anotherVCcancelBtn:(id)sender{
  [self.view removeFromSuperView];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"VCTblReload" object:nil];
}

我的问题是2 的 1。当我点击另一个VC的取消按钮时,它将进入VC屏幕,但VC没有响应 2。即使我使用过通知和[tableView reloadData];

,VC中的tableView也没有重新加载

对你的答案作出解释会很棒谢谢你

3 个答案:

答案 0 :(得分:0)

试试这个: -

 if (indexPath.row == 1)
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ContactUsVC *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"ContactUsVC"];
        [self presentViewController:vc animated:NO completion:nil];
    }

并且在此方法中也

-(void)openAnotherVc:(id)sender
{

            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ContactUsVC *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"ContactUsVC"];
            [self presentViewController:vc animated:NO completion:nil];
        }      

答案 1 :(得分:0)

使用Navigation Controller来做这种事情。然后Push Viewcontroller而不是添加到窗口和pop Viewcontroller而不是删除。添加和删​​除是UIView的良好做法。当它来到Viewcontrollers然后你应该使用NavigationController来管理导航堆栈。它会解决你的所有问题我认为。希望这会有所帮助。:)

更新: 如果您使用的是故事板,那么您也可以使用segue。它使整个流程更容易一些。谷歌它如何使用导航控制器与故事板。

答案 2 :(得分:0)

我的问题的答案是

SettingsVC.h
//UITableView Delegate method
if(indexPath.row==1){
VC=[[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"vcStoryBoardIdentifier"];
[VC.view setFrame:[[APP_DELEGATE window] frame]];
[[[[APP_DELEGATE window] rootViewController] view] addSubview:VC.view];
//[[self presentViewController:VC animated:NO completion:nil];
}


 VC.m
- (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view.

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTblPS) name:@"VCTblReload" object:nil];
        }

-(void)reloadTblPS{
   [VCTableView reloadData];
}

    //UITableview DataSource method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                static NSString *CellIdentifier = @"Cell";
                    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                    if(cell == nil)
                    {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                     if(indexPath.row == 0)
                        {
                button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setImage:img1 forState:UIControlStateNormal];
                [button setImage:img2 forState:UIControlStateHighlighted];
                [button setFrame:CGRectMake(220,10,60,20)];
                [button addTarget:self action:@selector(openAnotherVc:) forControlEvents:UIControlEventTouchUpInside];
                            [cell.contentView addSubview:button];
                }
                }
                return cell;

                }
-(void)openAnotherVc:(id)sender{

            anotherVC = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"anotherVCStoryBoardID"];
                    [[anotherVC view] setFrame:[[APP_DELEGATE window] frame]];
                    [[self presentViewController:anotherVC animated:NO completion:nil];]

            }
-(IBAction)VCcancelBtn:(id)sender{
                [self.view removeFromSuperview];
//[self dismissViewControllerAnimated:NO completion:nil];
            //working fine
            }


anotherVC.m
-(IBAction)anotherVCcancelBtn:(id)sender{
  [self dismissViewControllerAnimated:NO completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"VCTblReload" object:nil];
}