Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 正在父ViewController中获取当前ViewController名称_Iphone_Ios_Uiviewcontroller_Viewcontroller - Fatal编程技术网

Iphone 正在父ViewController中获取当前ViewController名称

Iphone 正在父ViewController中获取当前ViewController名称,iphone,ios,uiviewcontroller,viewcontroller,Iphone,Ios,Uiviewcontroller,Viewcontroller,我有如下视图控制器 父视图控制器 @interface Parent : UIViewController { } @interface Child : Parent { } 子视图控制器 @interface Parent : UIViewController { } @interface Child : Parent { } 现在我想在父视图控制器中显示当前的视图控制器名称,我已经搜索过了,但得到的简单线索是 如果是基于导航的应用程序,您可以通过 UIViewController

我有如下视图控制器

父视图控制器

@interface Parent : UIViewController
{
} 
@interface Child : Parent
{
}
子视图控制器

@interface Parent : UIViewController
{
} 
@interface Child : Parent
{
}
现在我想在父视图控制器中显示当前的视图控制器名称,我已经搜索过了,但得到的简单线索是

如果是基于导航的应用程序,您可以通过

UIViewController *currentVC = self.navigationController.visibleViewController;
但是,访问父viewcontroller中当前viewcontroller名称的当前过程是什么

试试这个

    for(UIViewController * view in self.navigationController.viewControllers)
    {
        if([view isKindOfClass:[Child class]])
        {
           //child VC is displayed on screen
        }
    }

下面的代码可能会有所帮助。我没有试过,但请告诉我是否有效

在父类中编写以下方法

 -(void)getClassName:(NSString*)className{
   NSLog(className);
   //Your code here
 }
需要访问当前viewcontroller名称的以下行

 [self getClassName:NSStringFromClass([self class])];

希望这篇文章有帮助

我找到了答案

在父视图控制器中:

- (void)viewDidLoad
{
    UIViewController *currentVC = self.navigationController.visibleViewController;
    NSLog(@"The Current Class : %@", [self getClassName:currentVC]);
}

- (NSString*) getClassName:(id) obj
{   
    const char* className = class_getName([obj class]);
    NSString *clsName = [@"" stringByAppendingFormat:@"%s",className];

    return clsName;
}
当我们从子视图控制器调用[super viewDidLoad]时,父视图控制器已初始化,这里我使用当前视图控制器的类名。因此,在我的应用程序中,我不想在每个视图控制器中编写这行代码,而是在父视图控制器中编写


注意:这只适用于基于导航的应用程序,因为我们从navigationController获取当前visibleViewController

您所说的“viewcontroller名称”是什么意思?您是指类名(例如
子类
还是
父类
)?您指的是视图控制器的标题(来自
标题
属性)?按父视图控制器您指的是父类吗@rmaddy我正在尝试在父类中获取当前ViewController的类名这是非常棒的代码!它非常适合我,谢谢!
@interface Parent : UIViewController
{
} 
@interface Child : Parent
{
}