代码之家  ›  专栏  ›  技术社区  ›  hfossli

在UIViewController中设置子视图

  •  1
  • hfossli  · 技术社区  · 16 年前

    编辑:这个问题是因为对类中的接口生成器和属性的工作方式缺乏了解。

    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 回复  |  直到 13 年前
        1
  •  2
  •   beefon    16 年前

    self.view

    - (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
    }
    
        2
  •  1
  •   Deepak Danduprolu    16 年前

    mySubview 是对UIView对象的引用的属性。所以当你分配一个 反对它,你只是在改变什么 mySubview 指的是,而不是本案中的,

    self.mySubview = anotherView;
    

    显示的原始UIView对象 was指的是在 看法 的 子视图

    但是当你加上 另一种观点 作为的子视图 另一种观点 属于视图层次结构,并显示在屏幕上。所以这是可行的。

    view (parent of) mySubview (parent of) anotherView
    

    但是,当您指定 另一种观点 直接到 ,您不仅可以更改UIView对象 是指,但它也将自身添加到父视图中。这是由 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;
    }
    



        3
  •  0
  •   hfossli    16 年前

    作为对@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]; 
    }
    
        4
  •  -1
  •   Jab    16 年前

    实例变量必须是属性才能使用点。语法,使用:

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

    在标题中,并使用:

    @synthesize subview;
    

    在主文件中。

    subview