2010-02-04 25 views
1

编辑设置:这个问题是由于缺乏大的理解界面生成器和属性类是如何工作的。在UIViewController的一个子视图

为什么我就不能设置像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:工作还好吧..

+0

对不起,我不太清楚。在interfacebuilder中作为selfview的子视图,我有mySubView。它作为一个IBOutlet连接到控制器中设置的属性。就像你可以在标题中看到的一样。 – hfossli 2010-02-04 13:08:40

回答

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 
} 
+0

聪明,但不是我想到的。我想设置self.view所持有的特定子视图。 – hfossli 2010-02-04 13:09:32

+0

你的意思是说你在UIViewController的视图中放了一些视图,并且你想用代码中的不同视图替换该视图(来自IB)?据我所知,你需要做的: 1.保存当前view.frame:CGRect mySubViewFrame = [mySubView框架]; 2.删除并放入新的子视图 - 我已经写了如何去做 3.为新视图设置新帧:[mySubView setFrame:mySubViewFrame]; – beefon 2010-02-04 15:43:50

+0

很多杂耍,但可以接受。我不会点击“接受”,因为我希望解释为什么或更简单的解决方案,但现在要坚持使用您的解决方案。谢谢 – hfossli 2010-02-05 12:46:18

-1

你的实例变量必须是一个属性,以使用点。语法,使用:

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

在报头中,并使用:

@synthesize subview; 
在主文件

为了使用点设置一个UIView。您需要使它成为一个属性的语法。这也可以让你在课堂外设置subview的属性。

+0

我真的不能看到你写的和什么之间的区别我写了...如果不同,你能解释一下吗? – hfossli 2010-02-04 14:29:56

+0

嗯,我正在用自己的方式来阐述。如果他接受你的话,我会删除我的答案 – Jaba 2010-02-04 14:43:01

0

至于什么@beefon所说的响应。这种方式和预期的一样,但背景颜色是透明的。它doesen't回应要么...按钮没有得到压制等。

- (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]; 
} 
1

mySubview是这是一个的UIView对象的引用的属性。所以,当你分配一个的UIView对象吧,你只是改变什么mySubview指的是并没有更多的在这种情况下,

self.mySubview = anotherView; 

原来的UIView对象mySubview指的是仍称为内视图子视图属性。没有什么变化。

但是当你添加anotherViewmySubviewanotherView属于视图层次结构,并在屏幕上显示的子视图。所以这个工作。

view (parent of) mySubview (parent of) anotherView 

但是当你将anotherView直接向视图,你不仅改变了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; 
} 



+0

我星期一给它一个旋转。非常感谢!看起来像是一个合适的解 – hfossli 2010-02-05 23:39:28

+0

这个解决方案和我的区别有什么区别? – beefon 2010-02-07 19:36:50

相关问题