簡體   English   中英

很難將UIView與作為子視圖添加到UIViewController

[英]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;
    }

您可能還需要實現MicBlowViewControllerloadView方法。

您可能需要查看《 View編程指南》以及UIViewController類參考

遇到同樣的問題。 幾乎所有示例代碼都使用視圖控制器和分別創建的視圖(我猜Xcode的工作方式)。 Xcode現在將ViewController與一個視圖一起創建為屬性(在GUI上看到的視圖)。 更重要的是,您無法訪問該視圖的代碼(確實,我被要求閱讀此手冊)。 因此,當您看不到示例代碼時,如何將其放入視圖中?

我發現了兩種方法:

  1. 創建與示例視圖中的代碼相同的新視圖。 然后,單擊Storyboard,在GUI中查看ViewController的默認視圖。 在右側窗格中,單擊身份檢查器(無意義的形狀從左起第三個)。 您將看到Class:UIView。 將UIView替換為您創建的視圖的名稱。 現在,ViewController將加載您的視圖。 另一個難題是,當您創建視圖時,它將獲得方法initWithFrame,該方法表示它運行了初始化代碼。 它不是。 您需要創建initWithCoder(在Google上搜索),並將您的init代碼放入此處。

  2. 另一個選項(可能是正確的方法)是使用ViewController代碼將您的視圖(帶有示例代碼)添加為默認視圖的子視圖,例如在ViewController.m中

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

    [self.view addSubview:newBus];

    即self是ViewController,而view是其默認視圖,即ViewController的屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM