正如标题所述,我正在尝试导入UIView,但我无法让它工作。 第一部分代码显示了我想要做的事情而不导入任何东西:
@interface ViewController : UIViewController
{
UIView *firstUIView;
UIView *secondUIView;
}
@end
@implementation ViewController
-(void)firstUIView
{
firstUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
firstUIView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstUIView];
}
-(void)secondUIView
{
secondUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
secondUIView.backgroundColor = [UIColor blueColor];
[self.view addSubview:secondUIView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self firstUIView];
[self secondUIView];
}
@end
现在以下一点不会给出相同的结果,但我想要它 - 我哪里出错了?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
firstUIViewExternal *firstUIView = [[firstUIViewExternal alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
secondUIViewExternal *secondUIView = [[secondUIViewExternal alloc] init];
[self.view insertSubview:firstUIView aboveSubview:self.view];
[self.view addSubview:secondUIView];
}
@end
firstUIViewExternal和secondViewExternal的代码相同,名称不同,如下所示:
-(void)secondUIView
{
secondUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
secondUIView.backgroundColor = [UIColor blueColor];
[self addSubview:secondUIView];
}
然后使用#imported导入它们,声明在上面的 - (void)viewDidLoad方法中使用。
为什么导入的版本不起作用?
如果需要,我会提供任何额外信息。 感谢: - )
这是我想要通过导入UIViews来实现的,但我得到一个空白的白色:
答案 0 :(得分:1)
我在代码中注意到了两件事:
您要导入的视图代码应如下所示:
·H:
#import <UIKit/UIKit.h>
@interface firstUIViewExternal : UIView
@end
的.m:
#import "firstUIViewExternal.h"
@implementation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor blueColor];
}
return self;
}
@end