0

タイトルにあるように、UIViewをインポートしようとしていますが、動作させることができません。コードの最初のビットは、何もインポートせずに私がやろうとしていることを示しています。

@interface ViewController : UIViewController
{

  UIView *firstUIView;
  UIView *secondUIView;

}

@end

@implementation ViewController

-(void)firstUIView
{
    firstUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    firstUIView.backgroundColor = [UIColor redColor];
    [self.view addSubview:firstUIView];
}

-(void)secondUIView
{
    secondUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
    secondUIView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:secondUIView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

    [self firstUIView];
    [self secondUIView];

}

@end

次のビットでは同じ結果は得られませんが、私はそれを望んでいます-どこが間違っているのですか?

- (void)viewDidLoad
   {
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

     firstUIViewExternal *firstUIView = [[firstUIViewExternal alloc]     initWithFrame:CGRectMake(0, 0, 320, 300)];
     secondUIViewExternal *secondUIView = [[secondUIViewExternal alloc] init];


     [self.view insertSubview:firstUIView aboveSubview:self.view];
     [self.view addSubview:secondUIView];

    }

    @end

firstUIViewExternalとsecondViewExternalのコードは同じですが、名前が次のように異なります。

-(void)secondUIView
 {
     secondUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
     secondUIView.backgroundColor = [UIColor blueColor];
     [self addSubview:secondUIView];
 }

次に、上記のように-(void)viewDidLoadメソッドで使用されると宣言された#importedを使用してインポートされます。

インポートしたバージョンが機能しないのはなぜですか?

必要に応じて追加情報を提供します。ありがとう:-)

これは、UIViewのインポートで実現したいことですが、空白の白いものが表示されます。

ここに画像の説明を入力してください

4

1 に答える 1

1

コードで気づいたことが 2 つあります。

  1. secondUIView のフレーム サイズを指定しません。
  2. 「UIViewExternal」ビューで initWithFrame メソッドをオーバーライドし、UIView から継承するだけで済みます。

インポートするビューのコードは次のようになります。

.h:

#import <UIKit/UIKit.h>
@interface firstUIViewExternal : UIView
@end

.m:

#import "firstUIViewExternal.h"
@implementation
   - (id)initWithFrame:(CGRect)frame
   {
        self = [super initWithFrame:frame];
        if (self) {
                // Initialization code
                self.backgroundColor = [UIColor blueColor];
        }
        return self;
    }
@end 
于 2012-09-13T22:25:13.580 に答える