29

jest możliwe, aby wyskoczyć widok ze stosu nawigacji, a następnie popchnąć na nie kolejny?popping i pchanie kontrolerów widoku w tej samej akcji

Próbuję zaimplementować płaską hierarchię dla tej sekcji i chciałbym mieć segmentowany kontroler, ale nie mogę sprawić, aby segmentowany kontroler wyglądał na coś, co mi się podobało, dlatego próbuję użyć kontrolera nawigacyjnego .

Po kliknięciu przycisku wykonałem ten kod:

[[self navigationController] popViewControllerAnimated:YES]; 
     MapsViewController *aViewController = [[MapsViewController alloc] 
               initWithNibName:@"MapsViewController" bundle:nil]; 
[self.navigationController pushViewController:aViewController animated:NO]; 
[aViewController release]; 

To pstrykaliśmy ok, ale tam nie ma śladu pchania! Każda pomoc będzie doceniona.

Odpowiedz

38
MapsViewController *aViewController = [[MapsViewController alloc] 
             initWithNibName:@"MapsViewController" bundle:nil]; 
    // locally store the navigation controller since 
    // self.navigationController will be nil once we are popped 
UINavigationController *navController = self.navigationController; 

    // retain ourselves so that the controller will still exist once it's popped off 
[[self retain] autorelease]; 

    // Pop this controller and replace with another 
[navController popViewControllerAnimated:NO];//not to see pop 

[navController pushViewController:aViewController animated:YES];//to see push or u can change it to not to see. 

Albo

MapsViewController *aViewController = [[MapsViewController alloc] 
             initWithNibName:@"MapsViewController" bundle:nil]; 


UINavigationController *navController = self.navigationController; 

//Get all view controllers in navigation controller currently 
NSMutableArray *controllers=[[NSMutableArray alloc] initWithArray:navController.viewControllers] ; 

//Remove the last view controller 
[controllers removeLastObject]; 

//set the new set of view controllers 
[navController setViewControllers:controllers]; 

//Push a new view controller 
[navController pushViewController:aViewController animated:YES]; 
+0

Nie chodzi tylko o to, że nie widzę tego, co się dzieje, to się nie dzieje. Przynajmniej nie ma dowodów, że to się stało. – Josh

+0

Jest to instancja MapViewController – Josh

+0

Mam zaktualizowany mój kod po prostu wklej go i uruchom –

26

Zrobione z roztworu https://stackoverflow.com/users/1619554/tomer-peled „s, tak aby inni mogli je znaleźć łatwiej.

To wydaje się być najlepszym sposobem, aby to zrobić dla systemów iOS 8:

UIViewController *newVC = [[UIViewController alloc] init]; // Replace the current view controller 
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]]; 
[viewControllers removeLastObject]; 
[viewControllers addObject:newVC]; 
[[self navigationController] setViewControllers:viewControllers animated:YES]; 
+1

To rozwiązanie działa na mnie z iOS 8, a nie wybraną odpowiedzią. Dzięki! – thefoyer

12

w Swift:

let newVc = UIViewController() 
var vcArray = self.navigationController?.viewControllers 
vcArray!.removeLast() 
vcArray!.append(newVc) 
self.navigationController?.setViewControllers(vcArray!, animated: false) 

W przypadku newVc istnieje w Storyboard:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let newVc = storyboard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as! UIViewController 
var vcArray = self.navigationController?.viewControllers 
vcArray!.removeLast() 
vcArray!.append(newVc) 
self.navigationController?.setViewControllers(vcArray!, animated: false) 
+0

Rodrigo, salvou meu dia. vlw! –

+0

Dzięki, zaoszczędziłem mój dzień :) –

4

Ty może użyć tego kodu do pop lub push kontrolera.

z obiektywnych c

bool alreadyPushed = false;  
//Check if the view was already pushed 
NSMutableArray *viewControllers; 
if ((viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers])) { 

    for (UIViewController *aViewController in viewControllers) { 

     if ([aViewController isKindOfClass:[YourControllerName class]]) { 
      NSLog(@"pop your view controller"); 
      [self.navigationController popToViewController:aViewController animated:YES]; 
      alreadyPushed = true; 
      break; 
     } 
    } 
} 

//Push Fresh View 
if(alreadyPushed == false) { 

    NSLog(@"push your view controller"); 
    YourControllerName *YourControllerObject = [[YourControllerName alloc]initWithNibName:@"YourNibName" bundle:nil];   
    [self.navigationController pushViewController:YourControllerObject animated:YES]; 

} 

Dla Swift

var alreadyPushed = false    
     //Check if the view was already pushed 
     if let viewControllers = self.navigationController?.viewControllers { 
      for viewController in viewControllers {      
       if let viewController = viewController as? YourControllerName {            
        self.navigationController?.popToViewController(viewController, animated: true);       
        print(" Push Your Controller") 
        alreadyPushed = true 
        break       
       } 
      } 
     }       
     if alreadyPushed == false {     
      let YourControllerObject = self.storyboard?.instantiateViewControllerWithIdentifier("YourControllerIdentifire") as! YourControllerName    
      self.navigationController?.pushViewController(YourControllerObject, animated: true) 

     } 
0
BOOL Present = NO; 

fifthViewController * fifthVC = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"]; 


for (UIViewController* viewController in self.navigationController.viewControllers) { 

    //This if condition checks whether the viewController's class is MyGroupViewController 
    // if true that means its the MyGroupViewController (which has been pushed at some point) 
    if ([viewController isKindOfClass:[fifthViewController class]]) 
    { 

     // Here viewController is a reference of UIViewController base class of MyGroupViewController 
     // but viewController holds MyGroupViewController object so we can type cast it here 
     fifthViewController *groupViewController = (fifthViewController*)viewController; 
     [self.navigationController popToViewController:groupViewController animated:NO]; 
     Present=YES; 
    } 

} 

if(Present==NO) 
{ 
    [self PushAnimation]; 
    [self.navigationController pushViewController:fifthVC animated:NO]; 

    Present=YES; 
} 
0

Swift 3,0

W przypadku gdy ktoś chce g O głąb hierarchii Widok:

   //Go back to desired viewController and then push another viewController 
       var viewControllers = self.navigationController!.viewControllers 
       while !(viewControllers.last is MyViewControllerClass) { 
        viewControllers.removeLast() 
       } 
       // go to new viewController 
       let anotherViewController = AnotherViewController(nibName: "AnotherViewController", bundle: nil) 
       viewControllers.append(anotherViewController) 
       self.navigationController?.setViewControllers(viewControllers, animated: true) 
1

Swift 4:

self.navigationController.setViewControllers[].. nie pracował dla mnie. Ale jestem w stanie rozwiązać problem, przytrzymując kontroler nawigacyjny w zmiennej instancji i wykonując operację push/pop. W ten sposób cicho jest w stanie zmienić kontroler bez usterki.

let navigationVC = self.navigationController 
    navigationVC?.popViewController(animated: false) 
    navigationVC?.pushViewController(myNewVC, animated: false) 
Powiązane problemy