29

là có thể bật một cái nhìn ra khỏi ngăn xếp chuyển hướng và sau đó đẩy khác thẳng vào nó?popping và đẩy xem bộ điều khiển trong cùng một hành động

Tôi đang cố gắng triển khai phân cấp phẳng cho phần này và muốn có bộ điều khiển được phân đoạn nhưng tôi không thể làm cho bộ điều khiển phân đoạn trông thích bất kỳ thứ gì tôi muốn, do đó tôi đang cố sử dụng bộ điều khiển .

Khi một nút được nhấp tôi thực thi mã này:

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

Nó popping tắt ok nhưng theres không có dấu hiệu của bất kỳ đẩy! Bất kỳ trợ giúp sẽ được đánh giá cao.

Trả lời

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. 

Hoặc

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

Nó không chỉ là tôi không thể thấy sự thúc đẩy xảy ra, nó không xảy ra. Ít nhất không có bằng chứng cho thấy nó đã xảy ra. – Josh

+0

Đó là một ví dụ của MapViewController – Josh

+0

tôi đã cập nhật mã của tôi chỉ cần dán nó và chạy –

26

Taken từ giải pháp https://stackoverflow.com/users/1619554/tomer-peled 's, để những người khác có thể tìm thấy nó dễ dàng hơn.

này dường như là cách tốt nhất để làm điều đó cho iOS8:

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

Giải pháp này làm việc cho tôi với iOS 8 chứ không phải câu trả lời đã chọn. Cảm ơn! – thefoyer

12

Trong Swift:

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

Trong trường hợp newVc tồn tại trong một 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

Cảm ơn, đã lưu ngày của tôi :) –

4

Bạn có thể sử dụng mã này để bật hoặc đẩy bộ điều khiển của bạn.

Đối với mục tiêu 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]; 

} 

Đối 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

Trong trường hợp ai đó muốn g o sâu vào quan điểm phân cấp:

   //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[].. không làm việc ra cho tôi. Nhưng tôi có thể giải quyết vấn đề bằng cách giữ một bộ điều khiển chuyển hướng trong một biến cá thể và thực hiện thao tác push/pop. Do đó, âm thầm có thể thay đổi bộ điều khiển mà không bị trục trặc.

let navigationVC = self.navigationController 
    navigationVC?.popViewController(animated: false) 
    navigationVC?.pushViewController(myNewVC, animated: false) 
Các vấn đề liên quan