2012-02-05 54 views
0

這聽起來很複雜,但我可以回答任何問題。我嘗試以非常複雜的方式執行的操作是我有兩個視圖,我試圖在我的歡迎屏幕主頁上加載。我想要加載的第一個是PageOne的視圖「text1Title」,所以頁面在awakeFromNib上調用了一個方法。現在,當我點擊下一個按鈕時,它告訴它隱藏下一個按鈕並再次運行第一頁方法,這樣它將使if語句有效並刪除text1View並加載第二頁。問題是,當我再次加載PageOne視圖時,它會創建視圖的兩個實例,removeFromSuperview只會刪除一個視圖。我怎樣才能使這個更簡化,並有這項工作?謝謝。刪除意見,以防止多個實例

@implementation WelcomeScreenMain 


    NSString *const text1Title  = @"WelcomeScreenText1"; 
    NSString *const text1Title2  = @"WelcomeScreenText2"; 


    - (void)awakeFromNib 
    { 
     [self PageOne]; 
    } 

    - (void) PageOne 
    { 
     //Page One 

     WelcomeScreenText1* text1View = 
     [[WelcomeScreenText1 alloc] initWithNibName:text1Title bundle:nil]; 
     // embed the current view 
     [myTargetView addSubview: [text1View view]]; 
     // make sure we automatically resize the controller's view to the current window size 
     [[text1View view] setFrame: [myTargetView bounds]]; 

     if (myButton.isHidden) { 
      [[text1View view] removeFromSuperview]; 
      [self PageTwo]; 
     } 


    } 

    - (void) PageTwo 
    { 
     //Page Two 

     WelcomeScreenText2* text2View = 
     [[WelcomeScreenText2 alloc] initWithNibName:text1Title2 bundle:nil]; 
     // embed the current view to our host view 
     [myTargetView addSubview: [text2View view]]; 
     // make sure we automatically resize the controller's view to the current window size 
     [[text2View view] setFrame: [myTargetView bounds]]; 
    } 

    -(IBAction)nextButton:(id)sender 
    { 

     myButton.hidden = YES; 
     [self PageOne]; 

    } 

    @end 

回答

1

在嘗試再次添加它之前,您可以刪除PageOne視圖的所有實例。

-(void)awakeFromNib 
{ 
    NSArray *subv = [NSArray arrayWithArray:myTargetView.subviews]; 
    for(NSView *view in subv) 
    { 
     if([view isKindOfClass:[WelcomeScreenText1 class]]) 
     { 
     [view removeFromSuperview]; 
     } 
    } 
    [self PageOne]; 
} 

您也可以在PageOne的開頭添加該片段。由你決定。

請注意,子視圖數組被複制,因爲你不能枚舉你可能會改變的東西。

無關的風格問題是,通常OBJ-C方法開始小寫所以pageOnePageOne