![](/img/trans.png)
[英]Make UIView subview as default UIView for UIViewController with Storyboard
[英]Hard time adding UIView with to a UIViewController as a subview
我基本上想使用Apple的MoveMe示例中的PlacardView.m和PlacardView.h,方法是将其作为子视图添加到我的主BlowViewController上
PlacardView.m
#import "PlacardView.h"
@implementation PlacardView
@synthesize placardImage;
- (id)init {
// Retrieve the image for the view and determine its size
UIImage *image = [UIImage imageNamed:@"Placard.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
// Set self's frame to encompass the image
self = [self initWithFrame:frame];
if (self) {
self.opaque = NO;
placardImage = image;
}
return self;
}
- (void)dealloc {
[placardImage release];
[super dealloc];
}
@end
PlacardView.h
@interface PlacardView : UIView {
UIImage *placardImage;
}
@property (nonatomic, retain) UIImage *placardImage;
// Initializer for this object
- (id)init;
@end
这是我的MicBlowViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
@class PlacardView;
@interface MicBlowViewController : UIViewController {
AVAudioRecorder *recorder;
NSTimer *levelTimer;
double lowPassResults;
PlacardView *placardView;
}
@property(nonatomic, retain) PlacardView *placardView;
- (void)setUpPlacardView;
- (void)levelTimerCallback:(NSTimer *)timer;
@end
这是部分MicBlowViewController.m ..有一个功能viewDidLoad,但与视图无关。.它只是一个用于音频录制的计时器,所以我没有粘贴
#import "MicBlowViewController.h"
#import "PlacardView.h"
@implementation MicBlowViewController
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setUpPlacardView];
}
return self;
}
- (void)setUpPlacardView {
// Create the placard view -- its init method calculates its frame based on its image
PlacardView *aPlacardView = [[PlacardView alloc] init];
self.placardView = aPlacardView;
[aPlacardView release];
placardView.center = self.center;
[self addSubview:placardView];
}
我得到的错误是“在类型“ MicBlowViewController *”的对象上找不到属性“中心””
请帮忙。
更改此行:
placardView.center = self.center;
对此:
placardView.center = self.view.center;
self
是UIViewController,但是您需要其视图的中心。
Ayush,您的代码使我有些困惑。
控制器的代码似乎有几个问题。
UIViewController没有initWithFrame
方法,而是标准的init
,或者,如果使用Interface Builder文件,则没有initWithNibName:bundle:
方法。
我看到您已经从Apple的MoveMe示例中复制并粘贴了代码,但是请记住,将代码从其复制到控制器中的MoveMeView
实际上是UIView而不是UIViewController 。
尝试这个:
#import "MicBlowViewController.h"
#import "PlacardView.h"
@implementation MicBlowViewController
- (void)viewDidLoad {
[self setUpPlacardView];
// Additonal code with your timer etc
}
- (void)setUpPlacardView {
// Create the placard view -- its init method calculates its frame based on its image
PlacardView *aPlacardView = [[PlacardView alloc] init];
self.placardView = aPlacardView;
[aPlacardView release];
[self.view addSubview:placardView];
self.placardView.center = self.view.center;
}
您可能还需要实现MicBlowViewController
的loadView
方法。
您可能需要查看《 View编程指南》以及UIViewController类参考 。
遇到同样的问题。 几乎所有示例代码都使用视图控制器和分别创建的视图(我猜Xcode的工作方式)。 Xcode现在将ViewController与一个视图一起创建为属性(在GUI上看到的视图)。 更重要的是,您无法访问该视图的代码(确实,我被要求阅读此手册)。 因此,当您看不到示例代码时,如何将其放入视图中?
我发现了两种方法:
创建与示例视图中的代码相同的新视图。 然后,单击Storyboard,在GUI中查看ViewController的默认视图。 在右侧窗格中,单击身份检查器(无意义的形状从左起第三个)。 您将看到Class:UIView。 将UIView替换为您创建的视图的名称。 现在,ViewController将加载您的视图。 另一个难题是,当您创建视图时,它将获得方法initWithFrame,该方法表示它运行了初始化代码。 它不是。 您需要创建initWithCoder(在Google上搜索),并将您的init代码放入此处。
另一个选项(可能是正确的方法)是使用ViewController代码将您的视图(带有示例代码)添加为默认视图的子视图,例如在ViewController.m中
Bus *newBus = [[Bus alloc] init];
[self.view addSubview:newBus];
即self是ViewController,而view是其默认视图,即ViewController的属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.