在同一操作中弹出和推送视图控制器

发布于 2024-11-26 23:46:24 字数 537 浏览 1 评论 0原文

是否可以从导航堆栈中弹出一个视图,然后将另一个视图直接推入其中?

我正在尝试为此部分实现一个平面层次结构,并且希望有一个分段控制器,但我无法使分段控制器看起来像我想要的那样,因此我尝试使用导航控制器。

单击按钮时,我执行了以下代码:

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

它弹出正常,但没有任何推动的迹象!任何帮助将不胜感激。

is is possible to pop a view off the navigation stack and then push another straight onto it?

I'm trying to implement a flat hierarchy for this section and would like to have a segmented controller but I can't make the segmented controller look anything liked I want, hence why I'm trying to use the navigation controller.

When a button is clicked I executed this code:

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

It's popping off ok but theres no sign of any pushing! Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

红衣飘飘貌似仙 2024-12-03 23:46:24
 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.

或者

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

Or

 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];
蓬勃野心 2024-12-03 23:46:24

在 Swift 中:

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

如果 Storyboard 中存在 newVc:

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)

In Swift:

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

In case newVc exists in a 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)
烟沫凡尘 2024-12-03 23:46:24

取自 https://stackoverflow.com/users/1619554/tomer-peled 的解决方案,以便其他人可以更容易找到它。

这似乎是 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];

Taken from https://stackoverflow.com/users/1619554/tomer-peled 's solution, so others can find it more easily.

This appears to be the best way to do it for 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];
水染的天色ゝ 2024-12-03 23:46:24

Swift 4 :

self.navigationController.setViewControllers[].. 不适合我。但我可以通过将导航控制器保存在实例变量中并执行推送/弹出操作来解决问题。因此,能够默默地更换控制器而不会出现故障。

  guard let navigationVC = self.navigationController else { return }  
    navigationVC.popViewController(animated: false)
    navigationVC.pushViewController(myNewVC, animated: false)

Swift 4 :

self.navigationController.setViewControllers[].. doesn't worked out for me. But I am able to solve the issue by holding a navigation controller in an instance variable and do push/pop operation. Thus, silently able to change controller without glitch.

  guard let navigationVC = self.navigationController else { return }  
    navigationVC.popViewController(animated: false)
    navigationVC.pushViewController(myNewVC, animated: false)
那些过往 2024-12-03 23:46:24

您可以使用此代码来弹出或推送控制器。

对于目标 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];

}

对于 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)

        }

You can use this code to pop or push your controller.

For objective 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];

}

For 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)

        }
孤独患者 2024-12-03 23:46:24
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;
}
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;
}
我偏爱纯白色 2024-12-03 23:46:24

Swift 3.0

如果有人想深入了解视图层次结构:

                //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)

Swift 3.0

In case someone wants to go deep into the view hierarchy:

                //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)
无悔心 2024-12-03 23:46:24

这可以通过在 UINavigationController 上添加扩展来实现,如下所示:

extension UINavigationController {
    
    func popAndPushViewController(_ viewController: UIViewController, animated: Bool) {
        pushViewController(viewController, animated: animated)
        var viewControllersCopy = viewControllers
        if viewControllersCopy.count-2 >= 0 {
            viewControllersCopy.remove(at: viewControllersCopy.count-2)
        }
        setViewControllers(viewControllersCopy, animated: false)
    }        
}

并使用 popAndPushViewController 代替 navigationControllerpushViewController 方法> UIViewController 上的实例。

举个例子:

let aViewController = UIViewController() // Create instance of appropriate VC
navigationController?.popAndPushViewController(aViewController, animated: true)

This can be achieved by adding an extension on UINavigationController as follows:

extension UINavigationController {
    
    func popAndPushViewController(_ viewController: UIViewController, animated: Bool) {
        pushViewController(viewController, animated: animated)
        var viewControllersCopy = viewControllers
        if viewControllersCopy.count-2 >= 0 {
            viewControllersCopy.remove(at: viewControllersCopy.count-2)
        }
        setViewControllers(viewControllersCopy, animated: false)
    }        
}

And use popAndPushViewController in place of pushViewController method of navigationController instance on UIViewController.

As an example:

let aViewController = UIViewController() // Create instance of appropriate VC
navigationController?.popAndPushViewController(aViewController, animated: true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文