iphone - Objective-C:NSClassFromString() 与导航 Controller 一起使用?

标签 iphone objective-c ios nsobject

我想使用 NSClassFromString() 将一个字符串传递给,以便确定将哪个类推送到导航 Controller 上。问题是,我想向此类传递一个值,这会导致错误,并且尝试推送 View Controller 会导致错误,因为 NSObject 不是 UIViewController 类型类。

Class screenClass = NSClassFromString(screen.name);
NSObject* newClass = [[screenClass alloc] init];
newClass.asset = asset; // Causes errors because asset not in NSObject
[self.navigationController pushViewController:newClass animated:YES];// newClass is an NSObject
[newClass release];

有没有更好的方法来完成我在这里尝试的事情?

最佳答案

你可以简单地做

UIViewController *newObject = (UIViewController *)[[screenClass alloc] init];

然后警告将被删除。

关于iphone - Objective-C:NSClassFromString() 与导航 Controller 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7143379/

相关文章:

ios - Swift:手势识别器未注册

ios - Google Maps iOS SDK 和 Storyboard

ios - 如何关闭 Xcode Undefined Behaviour Sanitizer

iphone - 带有 CGBitmapContext 的 Drawrect 太慢了

iphone - 布局管理器,还是有更好的解决方案?

iPhone 4 的视口(viewport)问题

iphone - 将 NSMutableArray 传递给另一个 View ,给出空值

objective-c - 在objective-c中用NSDate获取昨天

objective-c - 在 OS X 上获取活跃的网络连接

ios - 在 SwiftUI NavigationView 中呈现 UINavigationBar 的旧小标题