2013-08-22 111 views

回答

1

假设您推送新视图,您可以访问UINavigation堆栈以查看前一个视图之前的哪个视图。

Class aClass = [[[self.navigationController viewControllers] objectAtIndex:self.navigationController.viewControllers.count - 2] class]; 

if (aClass == [UIViewControllerA class]) 
    //Do something 
else if (aClass == [UIVIewControllerB class]) 
    //Do something else 

或创建你推到单一视图定制init()方法可以让你在一个变量传递哪个查看它是从哪里来的(对不起,真的罗嗦)。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil isFromViewA:(bool)isFromViewA 
+0

嗯......它没有工作 – Hive7

+0

哎呀, srry。更新我的答案,应该是viewControllers.count - 2而不是-1。 – random

0

我可能会写一个自定义的初始化方法和初始化期间传递沿线的东西。

会有不同的方法。这是的方式一个在

yourView.h

-(id)initWithType:(int)viewControllerType; 

另外创建int变量假设INT viewType;在yourView.h

在yourView.m文件

 -(id)initWithType:(int)viewControllerType{ 
     self = [super initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)]; 
     if (self) { 
      //custom init here 
      viewType = viewControllerType; 

    } 

你可以先定义的viewController为1和第二为2

所以当初始化该yourView在第一次的viewController。代码应该是这样的

yourView *newView = [yourView alloc] initWithType:1]; 

所以当初始化该yourView在第二个viewController。代码应该像

yourView *newView = [yourView alloc] initWithType:2]; 

现在,事情就好办了

if(viewType==1){ 
//do something particular for first view controller 
} 
if(viewType==2){ 
//do something particular for second view controller 
} 

如果它不工作。请分享您的代码...谢谢

+0

我把什么在此行'自我= [超级initWithFrame:方法CGRectMake(<#CGFloat x#>,<#CGFloat y#>,<#CGFloat宽度#>,<#CGFloat高度#>)];'作为浮筒 – Hive7

+0

也其中是否设置了这些行:'yourView * newView = [yourView alloc] initWithType:2];' – Hive7

+0

问题1:您声明视图的框架...例如[super initWithFrame:(0,0,200,200)] ; ...根据您的应用所需的任何帧 –

-2

我自己研究过。我创建了一个nsobject,然后当我离开控制器时,我将它添加到数组中,如果它存在,则更新它然后在下一个控制器中,如果对象中元素的值等于它所来自的控制器的名称它额外的事情