0

我是 iOS 的新手...在我的项目中,我正在使用addsubview...导航到其他视图

我有很多观点说1到7..

What i want is when i am on the 7th view or 6th view or 5th view i want to go back to the 2nd view on a button click..

我正在使用UINavigationController示例,但据我了解,我们只能转到上一个视图...

我使用了另一个示例,它将我带到root view..以下是代码

   NSArray *viewsToRemove = [self.view subviews];
    for (UIView *v in viewsToRemove) {
        [v removeFromSuperview];

那么我应该使用UINavigationController还是需要使用上述方法进行修改?

任何想法我怎么能做到这一点..

请帮忙!!!!

谢谢。

我目前没有使用导航控制器,而是使用 addsubview 方法......这是一个不好的做法,我是否特别需要导航控制......谢谢!

4

5 回答 5

3

您可以使用导航控制器的以下方法移动到特定视图:

popToViewController:animated:

有关文档,请参见此处

因此,如果您有一个UINavigationController *navCon并且想要移动到第二个视图,请使用以下代码:

UIViewController *vc = [navCon.viewControllers objectAtIndex:1];
[navCon popToViewController:vc animated:YES];
于 2013-06-26T12:42:35.997 回答
1

如果您想从第 5 个视图转到第 2 个视图,请使用以下代码:

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[allViewControllers removeObject:self];//removing 5th
[allViewControllers removeLastObject];//removing 4th
[allViewControllers removeLastObject];//removing 3rd
self.navigationController.viewControllers = allViewControllers;

现在只需弹出导航控制器

于 2013-06-26T12:42:35.007 回答
0

请尝试以下,

//This for loop iterates through all the view controllers in navigation stack.
for (UIViewController* viewController in self.navigationController.viewControllers) {

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

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

谢谢,

于 2013-06-26T12:48:13.830 回答
0
for (UIViewController *viewcontroller in self.navigationController.viewControllers)
    {
        if ([viewcontroller isKindOfClass:[self.navigationController class]])
        {
            UIStoryboard *story_profile=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

            FindFriends_ViewController *objc_prof = [story_profile instantiateViewControllerWithIdentifier:@"FindFriends_ViewController"];

            [self.navigationController popToViewController:objc_prof animated:YES];

        }
    }
于 2013-06-26T12:55:42.897 回答
0

甜蜜而简单的解决方案:只需在调用函数的地方编写以下代码:

- (IBAction)goToSecondViewController
{
   NSArray *array = [self.navigationController viewControllers];
   [self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
   // where i indicates viewController number,In your case write 2 instead of i.
}

希望这可以帮助你。

于 2013-06-26T13:08:07.820 回答