【发布时间】:2013-09-16 08:21:29
【问题描述】:
我创建了一个这样的类别:
#import "UIViewController+Dimensions.h"
@implementation UIView (Dimensions)
- (double) screenWidth2
{
return self.frame.size.width;
}
- (double) screenHeight2
{
return self.frame.size.height;
}
@end
但是当我尝试在导入它后在我的视图控制器中调用它时,我得到:
"Use of undeclared identifier screenWidth2;
有没有比我所在的每个视图控制器更好的方法来在一个地方获取屏幕宽度?
然后我在我的视图控制器中这样做了:
_registerButton.frame = CGRectMake(0.0, 0.0, [self screenWidth2] / 2.0 - 1.0, 60.0);
但应用程序编译运行并崩溃。该类别的.h文件是:
#import <UIKit/UIKit.h>
@interface UIViewController (Dimensions)
- (double) screenWidth2;
- (double) screenHeight2;
@end
【问题讨论】:
-
您确定在
Category's.h文件中也声明了- (double) screenWidth2方法吗? -
顺便说一句。 1)您应该为您在系统类的类别中提供的方法使用前缀。 2) 您应该使用 CGFloat 而不是 double 作为返回值,特别是现在一些 iOS 设备将在 64 位环境中运行。
标签: ios objective-c ios6 uiviewcontroller ios7