gpt4 book ai didi

ios - 很难将 UIView 添加到 UIViewController 作为 subview

转载 作者:行者123 更新时间:2023-11-28 18:43:25 27 4
gpt4 key购买 nike

我基本上想使用 Apple 的 MoveMe 示例中的 PlacardView.m 和 PlacardView.h,将其添加为我的主 BlowViewController 上的 subview

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 但它与 View 无关..它只是一个用于录音的计时器所以我没有粘贴它

#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 *”的对象上找不到属性‘center’”

请帮忙。

最佳答案

Ayush,您的代码让我有些困惑。

您的 Controller 代码似乎有几个问题。

UIViewController 没有 initWithFrame 方法,而是标准的 init 方法,如果使用接口(interface),则有 initWithNibName:bundle: 方法生成器文件。

我看到您已经从 Apple 的 MoveMe 示例中复制并粘贴了代码,但是请记住,您从中将代码复制到 Controller 中的 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;
}

您可能还需要实现 MicBlowViewControllerloadView 方法。

您可能想查看 View Programming Guide ,以及 UIViewController class reference .

关于ios - 很难将 UIView 添加到 UIViewController 作为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8405419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com