iphone - 在警报中显示当前 View Controller 的名称

标签 iphone ios objective-c xcode

如果屏幕上显示的 View Controller 是“PrimaryViewController”,我正在尝试显示警报我不确定如何获取 View Controller 的名称,然后将其转换为 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];
}

最佳答案

您可以使用 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];
}

关于iphone - 在警报中显示当前 View Controller 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16997194/

相关文章:

iphone - 真正简单的 CoreData 关系但返回 nil 和 null?

ios - UIProgressView 动画只能正常工作一次

iphone - CFAutoRelease() 类似 iOS6 上的行为

删除 Main.storyboard 后 iOS 应用程序崩溃

ios - iOS 7 和 iOS 8 上的不同框架高度

iphone - 始终居中 UIView

iphone - 检测应用程序的首次运行

iphone - 未调用 Sub-ViewController 上的 TouchesBegan

objective-c - 获取当前年份

objective-c - 关于Objective-C项目中#define语句的问题