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

时间:2012-07-02 23:06:41

标签: iphone

主vc中的一个主要vc和其他vcs以及其他vcs的定义。这些其他的vcs在他们的课堂上有uiviews。现在我如何将这些其他vcs作为子视图添加到主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秒后将first.view替换为UIImageView作为子视图。它给出了使用未声明标识符baseView的消息。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

//的.h

@interface ViewController : UIViewController {

UIView *baseView;

}

@property (nonatomic, retain) UIView *baseView;

// .m

@synthesize baseView;

制作这部分:

- (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];

制作本:

[baseView addSubview:first.view];

到此:

[self.baseView addSubview:first.view];

或者您仍然可以使用上述内容。

然后你就完成了。 :)

答案 1 :(得分:1)

您在内部声明了baseView - (void)viewDidLoad。它的范围仅限于该方法。将其声明为iVar。

@interface YourMainViewController : UIViewController
{
  UIView* baseView;
}

@property (nonatomic,retain) UIView* baseView;

你知道演习。