2012-01-19 56 views
0

我在我的主視圖控制器這種方法:無法addSubView

-(void)showDialog {  
    if (stopDialogController == nil) 
     stopDialogController = [[StopDialogController alloc] initWithNibName:@"StopDialog" bundle:nil WithStop:@"CLAS"]; 
    if (stopDialogController) 
     [stopDialogController presentWithSuperview:self.view withStopName:@"Evan Kimia"]; 
} 

是被執行的罰款,如果我從[自我的ShowDialog]相同的視圖控制器中調用它,但如果我傳遞的指針這個主類到另一個視圖控制器它被執行,但視圖不會被添加爲主視圖控制器的子視圖,就像它應該和我不知道爲什麼。下面是現在的超級視圖方法:

- (void)presentWithSuperview:(UIView *)superview withStopName:stopName; 
{ 
    NSLog(@"present w/ superview called."); 
    [superview addSubview:self.view]; 
    stopNameLabel.text=stopName; 

} 
+1

請確認蘇佩rview不是零。 –

+0

測試過,superview不是零 – jfisk

回答

0

我做了一個小例子項目,我無法重現您遇到的問題。在我最小的項目中,我有3個視圖控制器:MainViewController,SecondViewController和ThirdViewController。我會粘貼下面的相關方法,請檢查你的項目和這個例子之間的差異,也可以發佈更多的代碼是一個好主意。

MainViewController

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] init]; 
    thirdViewController.mainViewController = self; 
    [thirdViewController showDialogIndirect]; 
} 

-(void)showDialog {  
    if (secondViewController == nil) 
     secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil WithStop:@"CLAS"]; 
    if (secondViewController) 
     [secondViewController presentWithSuperview:self.view withStopName:@"Evan Kimia"]; 
} 

SecondViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil WithStop:(NSString *)stopString { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if(self){ 
     // init here 
    } 
    return self; 
} 

-(void)presentWithSuperview:(UIView *)superView withStopName:(NSString *)stopName { 
    NSLog(@"present %@ with superview %@ and stopName %@", self, superView, stopName); 
    self.view.backgroundColor = [UIColor redColor]; 
    [superView addSubview:self.view]; 
} 

ThirdViewController

-(void) showDialogIndirect { 
    [self.mainViewController showDialog]; 
}