简体   繁体   English

带有[self.view removeFromSuperview]的touchUpInside Back按钮时,上一个视图无响应

[英]Previous view unresponsive when touchUpInside Back button with [self.view removeFromSuperview]

I am new to iPhone Programming. 我是iPhone编程新手。

Basically I have a MFSideMenuViewController with settings viewController as rightSideViewController. 基本上我有一个MFSideMenuViewController,将viewController设置为rightSideViewController。

Settings ViewController is with UITableView and when I tap at any Index of tableview its loading another VC as [rootViewController.view addSubView:VC]; 设置ViewController与UITableView一起使用,当我点击tableview的任何Index时,它将加载另一个VC作为[rootViewController.view addSubView:VC]; with implemented back button code - [self.view removeFromSuperview]; 具有实现的后退按钮代码- [self.view removeFromSuperview]; until now its working fine 到现在为止,一切正常

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];
}

In this VC I have a tableview. 在这个VC中,我有一个tableview。 when I tap at any index path.row it is loading another VC named anotherVC as subview to VC [VC.view addSubView:anotherVC]; 当我点击任何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
            }

this anotherVC also containing a Cancel button with `[anotherVC.view removeFromSuperView]; 这个anotherVC还包含带有[[anotherVC.view removeFromSuperView]的取消按钮;

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

My problems are 2 here 1. When I click on cancel button of anotherVC it is going to VC screen but the VC is unresponsive 2. the tableView present in VC is not reloading even I have used notifications and [tableView reloadData]; 我的问题是2。1。当我单击anotherVC的“取消”按钮时,它将进入VC屏幕,但是VC没有响应 。2。即使我使用了通知和[tableView reloadData]; ,VC中存在的tableView也不会重新加载[tableView reloadData];

It would be great with explanation for your answer Thank you 很好的解释你的答案谢谢

try this:- 尝试这个:-

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

and in this method also 而且在这种方法中

-(void)openAnotherVc:(id)sender
{

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

Use Navigation Controller to do this kind of stuff. 使用Navigation Controller来执行此类操作。 then Push Viewcontroller instead of add to window and pop Viewcontroller instead of remove. 然后Push Viewcontroller而不是添加到窗口,然后pop Viewcontroller而不是删除。 add and remove are good practise for UIView . 添加和删​​除是UIView良好做法。 when it's comes to Viewcontrollers then you should use NavigationController to manage navigation stack. 当涉及到Viewcontrollers时,您应该使用NavigationController来管理导航堆栈。 it will resolve your all issue i think. 我认为它将解决您的所有问题。 hope this will help.:) 希望这会有所帮助。:)

Update : And if you are using storyboard then you can use segue also. 更新:如果您使用情节提要,那么您也可以使用segue。 it make whole flow little bit easier. 它使整个流程更加容易。 google it that how to use navigation controller with storyboard. 谷歌它如何与情节提要使用导航控制器。

Answer for my question is 我的问题的答案是

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];
}

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

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