2016-04-15 13 views
0

我是iPhone編程的新手。以前的觀點沒有反應當touchUpInside後退按鈕與[self.view removeFromSuperview]

基本上我有一個MFSideMenuViewController設置viewController作爲rightSideViewController。

設置視圖控制器是UITableView的,當我在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我有一個實現代碼如下。當我在任何index path.row點擊它加載另一個VC命名anotherVC作爲子視圖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 
      } 

此anotherVC還含有`一個取消按鈕[anotherVC.view removeFromSuperView];

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

我的問題是2這裏 1.當我點擊取消anotherVC的按鈕,它是將VC屏幕,但VC沒有響應 2.目前在VC中的tableView甚至不重裝我已經使用的通知和[tableView reloadData];

它將與解釋你的答案是偉大謝謝

+0

您可以發佈您的代碼。不完全得到你的問題。 – Lion

回答

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

TR Y本: -

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

它不工作。它給出了一個空白的白色屏幕,而不是以前的屏幕(VC) –

+0

你寫了(yoursubviewVC).subviews而不是self.view.subviews即yoursubviewVC是你想要隱藏的vc – 2016-04-15 10:02:49

+0

屏幕正在爲[self.view removeFromSuperview ] ;.那很酷。但是VC視圖沒有響應或者不相互作用。那就是我的問題 –

0

使用Navigation Controller做這種東西。然後Push Viewcontroller而不是添加到窗口和pop Viewcontroller而不是刪除。添加和刪​​除是UIView的良好做法。當涉及到Viewcontrollers時,您應該使用NavigationController來管理導航堆棧。它會解決你的所有問題,我認爲。希望這會有所幫助。:)

更新: 如果你正在使用故事板,那麼你也可以使用segue。它使整個流程更容易一點。谷歌它如何使用導航控制器與故事板。

+0

但在我的項目中,我沒有使用任何segues和導航控制器..我做了我的自己與不同的情況..一些什麼它的工作現在..謝謝@lion –

+0

使用導航控制器。這是你強迫不能使用的東西嗎? – Lion

+0

是的,它是..因爲數據來自服務..根據數據設置視圖控制器正在加載。所以我不能在這裏使用導航控制器 –