Iphone 尝试将视图控制器作为uiview加载到mainvc的子视图

Iphone 尝试将视图控制器作为uiview加载到mainvc的子视图,iphone,Iphone,一个主vc和其他vc以及主vc中其他vc的定义。这些其他风投在他们的类中有UIView。现在,我如何将这些其他vc作为子视图添加到主vc - (void)viewDidLoad{ UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; [self.view addSubview:baseView]; // Displays UIImageView UIImageView *myImage = [

一个主vc和其他vc以及主vc中其他vc的定义。这些其他风投在他们的类中有UIView。现在,我如何将这些其他vc作为子视图添加到主vc

- (void)viewDidLoad{
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
                     initWithImage:[UIImage imageNamed:@"ka1_046.png"]];
myImage.frame = CGRectMake(0, 0, 320, 460);
[baseView addSubview:myImage]; 
// create the UIToolbar at the bottom of the view controller
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 40)];
toolbar.barStyle = UIBarStyleBlackOpaque;
[baseView addSubview:toolbar];

//Add Play Button
self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.playButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
self.playButton.frame = CGRectMake(0, 0, 30, 30);
[self.playButton setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
 UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:self.playButton];

-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];

}else{
[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
 if(isFirstTime == YES)
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                        target:self
                                        selector:@selector(displayviewsAction:)
                                         userInfo:nil
                                         repeats:NO];
    isFirstTime  = NO;
}} }


- (void)displayviewsAction:(id)sender{  
 First *first = [[First alloc] init];
 first.view.frame = CGRectMake(0, 0, 320, 480);
 CATransition *transitionAnimation = [CATransition animation];   
 [transitionAnimation setDuration:1];
 [transitionAnimation setType:kCATransitionFade];
 [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
 [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
 [baseView addSubview:first.view];
 [self.view addSubview:toolbar];
 [first release];  
如果在displayviewaction中,我会这样做

[baseView addSubview:first.view];  
因此,根据playpauseAction,它在11秒后将UIImageView替换为first.view作为子视图。它给出了使用未声明标识符baseView的消息

谢谢你的帮助。

/.h

@interface ViewController : UIViewController {

UIView *baseView;

}

@property (nonatomic, retain) UIView *baseView;
//m

让这一部分:

- (void)viewDidLoad{
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[self.view addSubview:baseView];
为此:

- (void)viewDidLoad{
self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[self.view addSubview:self.baseView];
[self.baseView addSubview:first.view];

这样做:

[baseView addSubview:first.view];
为此:

- (void)viewDidLoad{
self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[self.view addSubview:self.baseView];
[self.baseView addSubview:first.view];
或者你仍然可以使用上面的方法


那你就完了。:)

您正在-(void)viewDidLoad内声明baseView。它的范围仅限于该方法。宣布它为iVar

@interface YourMainViewController : UIViewController
{
  UIView* baseView;
}

@property (nonatomic,retain) UIView* baseView;

你知道这个练习。

现在它将UIImageview替换为first.view,但是在替换为first.view之后,无论我将哪个视图控制器加载为UIImageview的子视图,它都应该始终显示UIImageview的uitoolbar。我想你必须手动将其放在nib中,或者将工具栏放在其中一个视图中,viewDidLoad或VIEWWILLAPPERIN mainvc的viewDidLoad已经有[self.baseView addSubview:toolbar];您添加的子视图似乎混淆了。你能解释清楚视图的层次结构吗。或者它应该如何显示?Mainvc有uitoolbar,没有navigationcontroller和tabbarcontroller。当按下uitoolbar上的播放按钮时,它将执行playpauseaction,这将进一步执行displayviewsaction,并开始根据不同的时间间隔逐个加载视图控制器视图