Ios 如何知道哪个视图被推送到某个视图

Ios 如何知道哪个视图被推送到某个视图,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,我的问题是: 我有3个不同的UIViewControllers 第一个是A,第二个是B,第三个是C A有一个到B的推序列,然后我在ViewDidLoad方法中做一些事情。 C还有一个到B的序列,然后i=我需要在ViewDidLoad方法中执行其他操作 有没有办法知道哪个UIViewControllerA或C推到了B 在类B中的.h文件中可以有如下属性: @property (nonatomic) NSString *viewControllerName; 在类A和类C中的-preparefor

我的问题是:

我有3个不同的
UIViewControllers

第一个是A,第二个是B,第三个是C

A有一个到B的推序列,然后我在
ViewDidLoad
方法中做一些事情。 C还有一个到B的序列,然后i=我需要在
ViewDidLoad
方法中执行其他操作


有没有办法知道哪个
UIViewController
A或C推到了B

在类B中的.h文件中可以有如下属性:

@property (nonatomic) NSString *viewControllerName;
在类A和类C中的
-prepareforsgue:
方法中,可以将
viewControllerName
属性设置为等于类A的名称或类C的名称。
然后,在类B的
viewDidLoad
中进行简单的if-else检查应该可以帮助您相应地加载它

示例B.h:

#import <UIKit/UIKit.h>

@interface B : UIViewController 

@property (nonatomic) NSString *view;

@end

类B中的.h文件中可以有如下属性:

@property (nonatomic) NSString *viewControllerName;
在类A和类C中的
-prepareforsgue:
方法中,可以将
viewControllerName
属性设置为等于类A的名称或类C的名称。
然后,在类B的
viewDidLoad
中进行简单的if-else检查应该可以帮助您相应地加载它

示例B.h:

#import <UIKit/UIKit.h>

@interface B : UIViewController 

@property (nonatomic) NSString *view;

@end

对。在您的导航堆栈中,您将能够将其作为推送当前视图的视图进行跟踪

NSArray*arr=[self.navigationController ViewControllers]


如果您打印他的数组,您将获得导航堆栈中所有viewCOntroller的回溯跟踪

是。在您的导航堆栈中,您将能够将其作为推送当前视图的视图进行跟踪

NSArray*arr=[self.navigationController ViewControllers]


如果您打印他的数组,您将得到B类的
.m
中导航堆栈中所有viewCOntroller的回溯,我们称之为
viewCOntroller\u B.m

#import "ViewController_A.h"
#import "ViewController_C.h"

//...

-(void)viewDidLoad
{
    //...

    NSArray *arrViewControllers = self.navigationController.viewControllers;

    if (arrViewControllers.count <= 1) { //not needed but just incase, maybe?
        NSLog(@"No parent");
        return;
    }

    id vcCurrent = arrViewControllers[arrViewControllers.count-2];
    if ([vcCurrent isKindOfClass:[ViewController_A class]]) {
        NSLog(@"Pushed by A");
        //Do class A specific things

    }
    else if ([vcCurrent isKindOfClass:[ViewController_C class]]) {
        NSLog(@"Pushed by C");
        //Do class C specific things

    }
}
#导入“ViewController_A.h”
#导入“ViewController_C.h”
//...
-(无效)viewDidLoad
{
//...
NSArray*ArrViewController=self.navigationController.ViewController;

如果在B类的
.m
中有(arrViewControllers.count,我们调用它,
ViewController\u B.m

#import "ViewController_A.h"
#import "ViewController_C.h"

//...

-(void)viewDidLoad
{
    //...

    NSArray *arrViewControllers = self.navigationController.viewControllers;

    if (arrViewControllers.count <= 1) { //not needed but just incase, maybe?
        NSLog(@"No parent");
        return;
    }

    id vcCurrent = arrViewControllers[arrViewControllers.count-2];
    if ([vcCurrent isKindOfClass:[ViewController_A class]]) {
        NSLog(@"Pushed by A");
        //Do class A specific things

    }
    else if ([vcCurrent isKindOfClass:[ViewController_C class]]) {
        NSLog(@"Pushed by C");
        //Do class C specific things

    }
}
#导入“ViewController_A.h”
#导入“ViewController_C.h”
//...
-(无效)viewDidLoad
{
//...
NSArray*ArrViewController=self.navigationController.ViewController;

如果(arrViewControllers.count ok),我会说得更具体一些。我有一个用户配置文件视图,其中几乎没有UITextField。在配置文件中,我有一个按钮,可以按下搜索视图,用户可以选择他居住的国家和城市。用户选择后,我需要更改为第一个视图中的值(a)因此,我需要知道哪个视图被推到A,因为OK会更具体。我有一个用户配置文件视图,其中几乎没有UITextField。在配置文件中,我有一个按钮,可以推到搜索视图,用户可以选择他居住的国家和城市。在用户选择后,我需要更改为第一个视图中的值(A)所以我需要知道哪个视图被推到了A,因为我很新。你能写一些示例代码吗?我会用这个解决方案。对不起,我很新。你能写一些示例代码吗?我会用这个解决方案。不是所有的视图控制器都有导航控制器。在我读了你的评论之后,我猜你需要从从一个视图控制器到下一个视图控制器是正确的吗???无论如何,即使我们正在演示或取消一个控制器,您也可以使用self.presentingViewController.parentviewcontroller来找出它的parentviewcontroller。并非所有的视图控制器都有导航控制器。根据我从您的评论中读到的内容,我猜您需要传递一些数据从一个视图控制器到下一个视图控制器是否正确???…任何方式,即使我们正在演示或取消一个控制器,您也可以使用self.presentingViewController.parentVIewCOntroller找出它的parentVIewCOntroller