2010-02-04 27 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

相關問題