我在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 :(得分:0)
而不是:
[homepage.view addSubview:roundedButtonType];
这样做:
[self.view addSubview:roundedButtonType];
答案 1 :(得分:0)
这段代码非常好:
HomePageViewController *homepage = [[HomePageViewController alloc] initWithNibName:@"HomePageViewController" bundle:nil];
[homepage.view addSubview:roundedButtonType];
您的按钮已添加到HomePageViewController
。
但HomePageViewController
未添加到任何地方,因此无法显示。你必须展示它。例如。在当前的ViewController上使用pushViewController
/ presentModalViewController
,或在窗口上使用setRootViewController
。
答案 2 :(得分:0)
您提供的数据不正确。可能是这两个原因,我已经给出了一些提示让我知道这不是你想要的。
首先
UI调用的更改必须从主线程发生。做一件事。运行[self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:]
二 主页。视图不可见。你可以给self.view(如果你从viewcontroller调用这个方法)。