简体   繁体   English

在导航控制器中按下并弹出?

[英]Push and pop in Navigation controller?

I am new to iPhone apps, I have a problem with navigation controller at push and pop view, for example I have three views i,e A , B and C respectively, in that I am going(push) to A-->B-->C and also coming back (pop) C-->B-->A its working. 我是iPhone应用程序的新手,在推和弹出视图中导航控制器存在问题,例如,我分别具有三个视图,即A,B和C,因为我要(推动)到A-> B -> C,然后回来(弹出)C-> B-> A。

case 1: Now my requirement is as follows A->C then i need to move from C->B so how can i do this? 情况1:现在我的要求如下A-> C,那么我需要从C-> B移走,那我该怎么做? in this case i'm using push(It's working). 在这种情况下,我正在使用push(正在工作)。

case 2: But when i'm going A->B->C and coming back from C->B i need to use pop NOT Push. 情况2:但是,当我要去A-> B-> C并从C-> B回来时,我需要使用pop NOT Push。 So How can use Push and pop operations in case1 sand case2. 因此,如何在case1和case2之间使用Push和pop操作。 Now i'm writing in the following way. 现在,我以以下方式编写。

    // A-->C and C-->B  or if [self.navigationController.viewControllers objectAtIndex:0]

I used this code: 我使用以下代码:

    CategoryClass *class=[[CategoryClass alloc]initWithNibName:@"CategoryClass" bundle:nil];    
    [self.navigationController pushViewController:class animated:YES];

    // A-->B-->C(push) for back C-->B or if [self.navigationController.viewControllers objectAtIndex:1] i want to use this code
    [self.navigationController popViewControllerAnimated:YES];

How can I compare whether the objectAtIndex:0 and objectAtIndex:1 如何比较objectAtIndex:0objectAtIndex:1

NSMutableArray *views=[NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[views removeObjectAtIndex:[views count]-2];
self.navigationController.viewControllers=views;
[self.navigationController popViewControllerAnimated:YES];

try this change the value instead of 2 i think this will help you 试试这个改变值,而不是2我认为这会帮助你

Delete the stack values and move to the page you want to....... 删除堆栈值并移至所需的页面。

thank you..... 谢谢.....

We welcome you, its not a problem that you are new, we are here to guide yu with proper solution, so dear fellow, first make some changes in appdelegate file which is: 我们欢迎您,这不是您的新问题,我们在这里为yu提供适当的解决方案,因此,亲爱的同伴,首先在appdelegate文件中进行一些更改:

storyboardName = [AppHelper isPhone] ? @"Main_iPhone" : @"Main_iPad";
storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
navController = (UINavigationController*)[storyboard instantiateInitialViewController];


     UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
     UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    MainMenu *controller = (MainMenu*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MainMenuiphone"];
    [navigationController pushViewController:controller animated:NO];
     self.window.rootViewController = navigationController;

and after that use this code for pushing views: 然后使用此代码推送视图:

 ANYVIEWCONTROLLER *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ANYVIEWZID"];
 [ [self navigationController] pushViewController:view animated:YES];

Look at my example: (read comments =) ) 看我的例子:(read comments =))

UINavigationController *navController;
UIViewController *viewControllerA, *viewControllerB, *viewControllerC;
[navController pushViewController:viewControllerA animated:YES]; // -> A
// some time passed
NSArray *currentViewControllers = [navController viewControllers]; // <- on stack now
NSMutableArray *newStack = [NSMutableArray arrayWithArray:currentViewControllers];
[newStack addObject:viewControllerB]; // <- adding B
[newStack addObject:viewControllerC]; // <- adding C
[navController setViewControllers:newStack animated:YES]; // <- setting new view controllers stack: A, B, C

// returning to B: pop viewControllerC
[navController popViewControllerAnimated:YES];
// or
[navController popToViewController:viewControllerB animated:YES];

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

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