显示警报中当前视图控制器的名称

Bra*_*don 1 iphone xcode objective-c ios

我试图显示一个警报,如果屏幕上显示的视图控制器是"PrimaryViewController"我不确定如何获取视图控制器的名称,然后将其转换为NSString ...任何帮助将非常感谢! !

//get name of current view controller
UIViewController *currentVC = self.navigationController.visibleViewController;
if ([currentVC isEqualToString:@"PrimaryViewController"])
{
    //display name of current view controller in alert    
    UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Your current view controller:" message:currentVC delegate:nil 
cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)

Bha*_*vin 5

你可以用NSStringFromClass().它将类的名称作为字符串返回.

示例代码:

if ([NSStringFromClass([currentVC class]) isEqualToString:@"PrimaryViewController"])
{
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"Your current view controller:" message:NSStringFromClass([currentVC class]) delegate:nil 
    cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)