如果屏幕上显示的 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/