简体   繁体   English

Interface Builder IB - 导航控制器,跳过视图

[英]Interface Builder IB - Navigation Controller, skipping a view

Hello I am new to iOS and have a question about navigating through my views. 您好我是iOS新手,对于浏览我的观点有疑问。

I am using IB wiring up the PREVIOUS and NEXT buttons in the nav bar that pushes my views. 我正在使用IB连接导航栏中的PREVIOUS和NEXT按钮来推送我的视图。 This all works fine. 一切正常。 However I am having trouble finding out where exactly what I need to do or where I need to place the code so I can skip over a view. 但是我无法找到我需要做什么或者我需要放置代码的位置,以便我可以跳过视图。 To simplify my situation... 为了简化我的情况......

-I have 1 Nav Controller and 4 View Controllers named VC1, VC2, VC3, VC4. - 我有1个Nav控制器和4个名为VC1,VC2,VC3,VC4的视图控制器。 Each VC has a .H/.M -Starting from VC1, they follow one after the other. 每个VC都有一个.H / .M-从VC1开始,它们一个接一个地跟着。

Say I want to skip VC3 and jump right to VC4 based on a setting in VC2. 假设我想跳过VC3并根据VC2中的设置直接跳转到VC4。 Where would I put the code to do this? 我会把代码放在哪里? Would I need to unhook the IBAction method from the NAV buttons at VC3? 我是否需要从VC3的NAV按钮取消IBAction方法?

I do apologize if this has been covered before. 如果之前已经涉及过,我会道歉。 If there is a tut or if you know of a post that answers this, please let me know. 如果有啧啧或者您知道有回复此帖的帖子,请告诉我。 I did do a search but the search was returning generic posts probably due to me using the wrong terminology. 我确实做了搜索,但搜索返回通用帖子可能是因为我使用了错误的术语。

Thanks in advance. 提前致谢。

A couple of thoughts: 几点想法:

  1. If you want to push from VC2 to either VC3 or to VC4, rather than having VC3 immediately push to VC4 in special cases, I think it's better to just have VC2 push directly to the appropriate view controller. 如果你想从VC2推送到VC​​3或VC4,而不是让VC3在特殊情况下立即推送到VC​​4,我认为最好让VC2直接推送到适当的视图控制器。 Thus you might have an IBAction in VC2 that would do this for you: 因此,您可能在VC2中有一个IBAction可以为您执行此操作:

     - (IBAction)pushToNext:(id)sender { BOOL skipToVC4 = ... // put in whatever logic you'd use to bypass VC3 and go directly to VC4 UIViewController *nextController; if (skipToVC4) { nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"VC4"]; // obviously, if using NIBs, you'd do something like: // nextController = [[ViewController4 alloc] initWithNibName:@"VC4" bundle:nil]; } else { nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"VC3"]; } [self.navigationController pushViewController:nextController animated:YES]; } 

    This way, when you pop back from VC4, you'll pop back directly to the appropriate view controller (eg if you pushed from VC2 to VC4, when you pop, you'll pop right back to VC2 automatically. 这样,当你从VC4回弹时,你会直接回到相应的视图控制器(例如,如果你从VC2推送到VC​​4,当你弹出时,你会自动弹回到VC2。

    And, obviously, if you're using storyboards, rather than manually invoking pushViewController , you could have two segues from VC2 (one to VC3 and one to VC4), give them appropriate identifiers, and then just invoke performSegueWithIdentifier to segue to the appropriate view controller. 而且,显然,如果你使用的是故事板,而不是手动调用pushViewController ,你可以从VC2中获得两个segue(一个到VC3,一个到VC4),为它们提供适当的标识符,然后只需调用performSegueWithIdentifier来转换到相应的视图控制器。 But the idea is the same: You can define an IBAction that performs the appropriate segue depending upon whatever logic you so choose. 但是这个想法是一样的:你可以根据你选择的逻辑来定义一个执行适当segue的IBAction

  2. You say that you have "PREVIOUS and NEXT buttons in the nav bar that pushes my views", I wonder about your "PREVIOUS" button. 你说你的导航栏中有“PREVIOUS和NEXT按钮推动我的观点”,我想知道你的“PREVIOUS”按钮。 Is that doing a popViewControllerAnimated ? 那是在做一个popViewControllerAnimated吗? Generally, a "NEXT" button will push to a new view controller, but the "PREVIOUS" button should not push to the previous view, but pop back to it. 通常,“NEXT”按钮将推送到新的视图控制器,但“PREVIOUS”按钮不应该推到上一个视图,而是弹回到它。 If you don't pop back, you can end up with multiple instances of some of your prior view controllers. 如果不弹回,则最终可能会出现一些先前视图控制器的多个实例。 Thus, the "PREVIOUS" button should be linked to an IBOutlet that does something like: 因此,“PREVIOUS”按钮应链接到IBOutlet ,它执行以下操作:

     - (IBAction)popToPrevious:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } 
  3. When popping back, you'll obviously pop back to the view controller that you pushed from. 弹回时,您显然会弹回到您推送的视图控制器。 If you want to skip a few of the view controllers as you're popping back in iOS versions prior to 6.0, you would use popToViewController or popToRootViewControllerAnimated . 如果您想要在6.0之前的iOS版本popToViewController回一些视图控制器,则可以使用popToViewControllerpopToRootViewControllerAnimated For example, let's say that you pushed from VC1 to VC2, to VC3, to VC4. 例如,假设您从VC1推送到VC​​2,再推送到VC​​3,再推送到VC​​4。 If you want to pop back from VC4 all the way to VC1, you would hook up and IBAction in VC4 like: 如果你想从VC4一直弹回到VC1,你可以在VC4中连接和IBAction ,如:

     - (IBAction)popToRoot:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } 

    Or, if you wanted to pop back from VC4 to VC2, you would 或者,如果你想从VC4回弹到VC2,你会的

     - (IBAction)popToVC2:(id)sender { for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[ViewController2 class]]) { [self.navigationController popToViewController:controller animated:YES]; return; } } } 

    You can avoid this iteration through the navigationController.viewControllers if you passed a reference of VC2 to VC3 and then again to VC4, but sometimes the above technique is easier. 如果您将VC2的引用传递给VC3然后再传递给VC4,则可以通过navigationController.viewControllers避免此迭代,但有时上述技术更容易。

    By the way, if you're supporting iOS 6 and above, only, and are using storyboards, you can also use unwind segues, which are a more elegant way of popping back to a particular view controller. 顺便说一下,如果你只支持iOS 6及以上版本,并且正在使用故事板,你也可以使用展开segue,这是一种更优雅的方式,可以弹回特定的视图控制器。 But it's not clear whether (a) you're using storyboards; 但目前尚不清楚(a)你是否正在使用故事板; and (b) you're supporting iOS 6 and above only, so I'll refrain from a discussion of unwind segues at this point. (b)你只支持iOS 6及更高版本,因此我将在此时不再讨论unwind segues。

First: 第一:
Show us some code, where you are pushing new view controllers, maybe your whole navigation controller code 向我们展示一些代码,您可以在其中推送新的视图控制器,也许是整个导航控制器代码

Second (Solution) : 第二(解决方案)

I assume: 我假设:

  • Your prev/next buttons are linked to your navigationController class 您的上一个/下一个按钮链接到您的navigationController类
  • you have the appropriate methods (prevPressed:/nextPressed:), which are called, when you click one of the buttons 当你单击其中一个按钮时,你有适当的方法(prevPressed:/ nextPressed :),它们被调用

I can help you with the following: 我可以帮助您解决以下问题:

  • you know which controller is visible at the moment with the visibleViewController @property 你知道哪个控制器目前可以通过visibleViewController @property看到
  • each time you click on a button in the navBar you can ask the visibleViewController which next/previous view controller should be pushed/popped 每次单击navBar中的按钮时,都可以询问visibleViewController应该按下/弹出哪个下一个/上一个视图控制器

Best solution would be, if all of your controllers VC1/2/3/4 are a subclass of a viewController class, which defines a method in it's interface: 如果所有控制器VC1 / 2/3/4都是viewController类的子类,那么最好的解决方案就是在它的接口中定义一个方法:

- (Class)nextViewControllerClass;
- (Class)previousViewControllerClass;

and in the implementation: 并在实施中:

- (Class)nextViewControllerClass {
    return [VC4 class];
}

- (Class)previousViewControllerClass {
    return [VC1 class];
}

And in your navigationController code the do this: 在您的navigationController代码中执行以下操作:

- (IBAction)next:(id)sender {
    UIViewController *nextViewController = [[[self.visibleViewController nextViewControllerClass] alloc] init];
    [self pushViewController:nextViewController animated:YES];
}

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

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