简体   繁体   中英

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

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

This is my 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

#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 *""

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.

Ayush, your code leaves me somewhat puzzled.

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.

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 .

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 .

You may want to check out View Programming Guide , as well as UIViewController class reference .

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 now creates ViewController together with a view as a property (the view you see on the 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. In the right hand pane, click on the identity inspector (meaningless shape third from left). You will see Class:UIView. Replace the word UIView with the name of the view that you created. The ViewController will now load your view. The other gotcha is that when you create the view it gets method initWithFrame which says it runs init code. It does not. You need to create initWithCoder (google this) and put your init code in here.

  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

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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