1

編集: この質問は、Interface Builder とクラスのプロパティがどのように機能するかを理解していないことが原因です。

self.mySubView = anoterhView;設定できるように設定できないのはなぜself.view = anotherView;ですか?

## .h
@interface TestController : UIViewController {
    IBOutlet UIView *mySubView;
}

@property (nonatomic, retain) IBOutlet UIView *mySubView;

##.m

@implements TestController

@synthesize mySubView;

- (void)viewDidLoad { 

    AnotherController *anotherController = [[AnotherController alloc] initWithNibName:nil bundle:nil];
    anotherView = anotherController.view;

    // if i do
    self.view = anotherView;
    // result: replaces whole view with anotherView

    // if i instead do
    self.mySubView = anotherView;
    // result: no change at all

    // or if i instead do:
    [self.mySubView addSubview:anotherView];
    // result: mySubView is now displaying anotherView

}

注: 私は interfacebuilder を使用しています。self.view と self.mySubView addSubview: が正常に動作しているため、すべてが正常に接続されていると確信しています..

4

4 に答える 4

2

自動的に表示されるようにするにはself.view、setter メソッドを上書きする必要があります。

- (void)setMySubView:(UIView *)view {
    [mySubView removeFromSuperview];  // removing previous view from self.view
    [mySubView autorelease];
    mySubView = [view retain];
    [self.view addSubview: mySubView]; // adding new view to self.view
}
于 2010-02-04T13:02:13.660 に答える
1

mySubviewは、UIView オブジェクトへの参照であるプロパティです。したがって、 UIViewオブジェクトをそれに割り当てると、 mySubviewが参照しているものを変更するだけで、この場合のようにはなりません。

self.mySubview = anotherView;

mySubviewが参照していた元の UIView オブジェクトは、引き続きviewsubviewsプロパティ内で参照されます。何も変わりません。

しかし、 mySubview のサブビューとして anotherView を追加するanotherViewビュー階層に属し、画面に表示されます。したがって、これは機能します。

view (parent of) mySubview (parent of) anotherView

ただし、anotherViewをviewに直接割り当てると、ビューが参照していた UIView オブジェクトを変更するだけでなく、それ自体をparentViewに追加します。これはUIViewControllerによって処理されます。

self.view = anotherView;



あなたのsetCurrentViewは、多かれ少なかれこのようにする必要があります。

- (void) replaceSubview:(UIView *)newView {
  CGRect frame = mySubview.frame;

  [mySubview removeFromSuperview];
  self.mySubview = newView;

  [self.view addSubview:newView];
  newView.frame = frame;
}



于 2010-02-05T16:50:27.823 に答える
0

@beefonが言ったことへの応答として。これは期待どおりに機能しますが、背景色は透明です。反応しない…ボタンが押せない…などなど。

- (void)setCurrentView:(UIView *)newView {
    /*      1. save current view.frame: CGRect mySubViewFrame = [mySubView frame]; 
            2. remove and put new subview - I have wrote how to do it 
            3. set new frame for new view: [mySubView setFrame:mySubViewFrame];      */ 
    CGRect currentViewFrame = [currentView frame];
    [currentView removeFromSuperview];
    [currentView autorelease];
    currentView = [newView retain];
    [self.view addSubview:currentView];
    [currentView setFrame:currentViewFrame]; 
}
于 2010-02-05T15:05:30.437 に答える
-1

ドットを使用するには、インスタンス変数がプロパティである必要があります。構文、使用:

@Property (nonatomic, retain) IBOutlet UIView* subview;

ヘッダーで使用し、次を使用します。

@synthesize subview;

メインファイル内。

ドットを使用してUIViewを設定します。プロパティにするために必要な構文。subviewこれにより、クラスの外部でのプロパティを設定することもできます。

于 2010-02-04T14:01:47.443 に答える