简体   繁体   English

很难将UIView与作为子视图添加到UIViewController

[英]Hard time adding UIView with to a UIViewController as a subview

I basically want to use the PlacardView.m and PlacardView.h from the Apple's MoveMe example, by adding it as a subview on my main BlowViewController 我基本上想使用Apple的MoveMe示例中的PlacardView.m和PlacardView.h,方法是将其作为子视图添加到我的主BlowViewController上

PlacardView.m 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 PlacardView.h

@interface PlacardView : UIView {
    UIImage *placardImage;

}

@property (nonatomic, retain) UIImage *placardImage;

// Initializer for this object
- (id)init;

@end

This is my MicBlowViewController.h 这是我的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

This is the partial MicBlowViewController.m .. there is a function viewDidLoad but that has nothing to do with the views.. its simply a timer for audio recording so I am not pasting that 这是部分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];
    }

The error I get is "Property 'center' not found on object of type "MicBlowViewController *"" 我得到的错误是“在类型“ MicBlowViewController *”的对象上找不到属性“中心””

Please help. 请帮忙。

Change this line: 更改此行:

placardView.center = self.center; 

to this: 对此:

placardView.center = self.view.center; 

self is the UIViewController, but you want the center of its view. self是UIViewController,但是您需要其视图的中心。

Ayush, your code leaves me somewhat puzzled. Ayush,您的代码使我有些困惑。

The code for your controller seems to have several problems. 控制器的代码似乎有几个问题。

A UIViewController does not have an initWithFrame method, but rather a standard init , or an initWithNibName:bundle: method if using an Interface Builder file. UIViewController没有initWithFrame方法,而是标准的init ,或者,如果使用Interface Builder文件,则没有initWithNibName:bundle:方法。

I see you have copied and pasted code from Apple's MoveMe example, however, bear in mind that the MoveMeView from which you have copied the code into your controller is actually a UIView and not a UIViewController . 我看到您已经从Apple的MoveMe示例中复制并粘贴了代码,但是请记住,将代码从其复制到控制器中的MoveMeView实际上是UIView而不是UIViewController

Try this: 尝试这个:

#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;
    }

You will probably also need to implement the loadView method of your MicBlowViewController . 您可能还需要实现MicBlowViewControllerloadView方法。

You may want to check out View Programming Guide , as well as UIViewController class reference . 您可能需要查看《 View编程指南》以及UIViewController类参考

Had same problems. 遇到同样的问题。 Nearly all sample code uses view controllers and views that were created separately (the way that Xcode used to work I guess). 几乎所有示例代码都使用视图控制器和分别创建的视图(我猜Xcode的工作方式)。 Xcode now creates ViewController together with a view as a property (the view you see on the GUI). Xcode现在将ViewController与一个视图一起创建为属性(在GUI上看到的视图)。 The kicker is that you cannot access the code for this view (afaict... I asked and got told to read the manual). 更重要的是,您无法访问该视图的代码(确实,我被要求阅读此手册)。 So how can you get the sample code (which is in a view) into this view when you can't see it's code? 因此,当您看不到示例代码时,如何将其放入视图中?

Two ways I have found: 我发现了两种方法:

  1. Create new view that is the same as code in sample views. 创建与示例视图中的代码相同的新视图。 Then, click on the Storyboard, in GUI you are looking at the ViewController's default view. 然后,单击Storyboard,在GUI中查看ViewController的默认视图。 In the right hand pane, click on the identity inspector (meaningless shape third from left). 在右侧窗格中,单击身份检查器(无意义的形状从左起第三个)。 You will see Class:UIView. 您将看到Class:UIView。 Replace the word UIView with the name of the view that you created. 将UIView替换为您创建的视图的名称。 The ViewController will now load your view. 现在,ViewController将加载您的视图。 The other gotcha is that when you create the view it gets method initWithFrame which says it runs init code. 另一个难题是,当您创建视图时,它将获得方法initWithFrame,该方法表示它运行了初始化代码。 It does not. 它不是。 You need to create initWithCoder (google this) and put your init code in here. 您需要创建initWithCoder(在Google上搜索),并将您的init代码放入此处。

  2. The other option (probably the correct way) is to use the ViewController code to add your view (with the sample code) as a subview of the default view eg in ViewController.m 另一个选项(可能是正确的方法)是使用ViewController代码将您的视图(带有示例代码)添加为默认视图的子视图,例如在ViewController.m中

    Bus *newBus = [[Bus alloc] init];

    [self.view addSubview:newBus];

    ie self is the ViewController and view is its default view ie a property of ViewController 即self是ViewController,而view是其默认视图,即ViewController的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM