0

我正在以其他方法添加子视图,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

}

谁能让我知道我是否做得对以及我错过了什么?

提前致谢。

4

3 回答 3

0

而不是这个:

[homepage.view addSubview:roundedButtonType];

做这个:

[self.view addSubview:roundedButtonType];
于 2012-06-04T08:59:06.390 回答
0

这段代码非常好:

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

You 按钮被添加到HomePageViewController.

但是HomePageViewController没有添加到任何地方,因此它不可见。你必须展示它。例如,在您当前的 ViewController 或Window 上使用pushViewController/ 。presentModalViewControllersetRootViewController

于 2012-06-04T09:02:29.100 回答
0

您提供的数据不正确。可能是这两个原因我已经给出了一些提示,让我知道这不是你想要的。

首先 UI 调用的变化必须从主线程发生。做一件事。运行中的方法[self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:]

第二 homepage.view 不可见。您可以提供 self.view(如果您从视图控制器调用此方法)。

于 2012-06-04T09:05:35.437 回答