我正在尝试为 IOS 6 添加 UIRefreshControl。
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
和
if (NSClassFromString(@"UIRefreshControl") != Nil) {
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
但是收到错误
dyld: Symbol not found: _OBJC_CLASS_$_UIRefreshControl
Referenced from: /Users/office/Library/Application Support/iPhone Simulator/4.3.2/Applications/DD532E42-77F9-471C-AA48-7F9EAE9268C6/Verizon.app/Verizon
Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/i PhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit
我正在使用IOS 6 SDK并在iPhone 4.3模拟器上运行。
当我删除代码时
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
一切都适用于iPhone 4.3模拟器。有趣的是里面的代码
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) { }
在iPhone 4.3模拟器上永远不会被调用,不知道为什么错误。请帮忙!!
一定要选择第二种风格,即:
if (NSClassFromString(@"UIRefreshControl") != nil) {
...
}
至于您遇到的错误,您应该能够通过在项目"构建阶段"设置的"将二进制文件与库链接"部分中将UIKit设置为"可选"来阻止它。
它应该将其标记为可选的,因为它注意到您正在使用仅在iOS 6中可用的类,但由于某种原因看起来它不适合您。
关系,代码不会被调用 - 当应用程序加载时,动态链接器需要解析应用程序的二进制文件链接到的动态库中的所有符号。在 iOS 4.3 中,没有实现 UIRefreshControl
类,因此在运行此操作系统(以及任何早于 iOS 6 的操作系统)的设备上,操作系统本身(好吧,而不是它的 UIKit 框架)不包含该类和与之对应的符号,因此应用程序甚至无法启动,即使它不使用仅适用于 iOS6 的代码。
您还需要知道预处理器宏是在编译时计算的,如果您的目标是 iOS 6,则您有条件编译的代码将被编译,无论您在哪个版本的 iOS 上运行程序,系统都会尝试执行该代码。
解决方案:不要使用条件编译,而是使用反射和自省来找出该类在运行时是否可用:
Class UIRefreshControl_Class;
if ((UIRefreshControl_Class = objc_getClass("UIRefreshControl")) != Nil) {
// class available
id control = [[UIRefreshControl_class alloc] init];
// etc.
}