2012-06-04 18 views
0

我在viewDidLoad()以外的方法中添加子視圖,並且不顯示子視圖。 我的代碼是在這裏如下:在viewDidLoad以外的方法中添加子視圖

-(void) displayBanner 
{ 

    SharedApp *instance=[SharedApp sharedInstance]; 
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES]; 

numTimerTicks = 0; 

    roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain]; //Creates a UIButton 
    roundedButtonType.frame = CGRectMake(0.0f, 360.0f, 320.0f, 60.0f); //sets the coordinates and dimensions of UIButton 
    roundedButtonType.backgroundColor = [UIColor clearColor]; //sets the background color 
    roundedButtonType.tag = numTimerTicks; 
    // [instance configView]; 
    NSLog(@"the home array is %@",instance.homeImageArray); 
    [roundedButtonType setBackgroundImage:[instance.homeImageArray objectAtIndex:numTimerTicks] forState:UIControlStateNormal]; 
    [roundedButtonType addTarget:self action:@selector(showDetails:)forControlEvents:UIControlEventTouchUpInside]; //sets the Background image 
    [roundedButtonType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

    HomePageViewController *homepage = [[HomePageViewController alloc]initWithNibName:@"HomePageViewController" bundle:nil]; 
    [homepage.view addSubview:roundedButtonType]; 


    //Oops forgot to add this one... my bad.. :D :D 
    [self.view addSubview:homepage.view];// Image displayed but goes into endless loop 

} 

任何人都可以讓我知道如果我已經做了正確的,什麼我已經錯過?

在此先感謝。

回答

0

,而不是這樣的:

[homepage.view addSubview:roundedButtonType]; 

做到這一點:

[self.view addSubview:roundedButtonType]; 
0

此代碼是完全正常:

HomePageViewController *homepage = [[HomePageViewController alloc] initWithNibName:@"HomePageViewController" bundle:nil]; 
[homepage.view addSubview:roundedButtonType]; 

您按鈕添加到HomePageViewController

但是HomePageViewController沒有添加到任何地方,所以它不可見。你必須展示它。例如。與您當前的ViewController上的pushViewController/presentModalViewController或窗口上的setRootViewController

+0

謝謝。我試過這樣。請讓我知道它是否正確。 'HomePageViewController * homepage = [[HomePageViewController alloc] initWithNibName:@「HomePageViewController」bundle:nil]; [self presentModalViewController:homepage animated:YES]; [homepage.view addSubview:roundedButtonType];' – user1268938

0

您提供的數據不正確。可能是這兩個原因,我已經給了一些提示讓我知道這不是你想要的。

第一個 UI調用的改變必須發生在主線程中。做一件事。運行[self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:]的方法

第二個 homepage.view不可見。你可以給self.view(如果你從視圖控制器調用這個方法)。

相關問題