2012-07-02 69 views
0

一個主要VC等VCS和在主VC其它VC的定義。這些其他vcs在他們的課上有uiviews。現在,我怎麼這些其他的VCS作爲子視圖添加到主VC試圖加載視圖控制器的UIView到子視圖mainvc的

- (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它取代的UIImageView 11秒鐘後子視圖first.view。它給出使用未聲明的標識符baseView的消息。

感謝您的幫助。

回答

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

,或者您還可以使用上面。

然後你就完成了。 :)

+0

現在它與first.view取代的UIImageView但first.view替換後它仍然應該顯示下面總是的UIImageView的無論uitoolbar哪個視圖控制器I M作爲加載的的UIImageView子視圖。 – user1452248

+0

我認爲你必須手動把它的筆尖或將工具欄或者viewDidAppear,viewDidLoad中或viewWillAppear中 – Bazinga

+0

的在mainvc的viewDidLoad中已經有[self.baseView addSubview:工具欄]。 – user1452248

1

您在聲明中基本視點 - (無效)viewDidLoad中。這種方法範圍有限。聲明它爲iVar。

@interface YourMainViewController : UIViewController 
{ 
    UIView* baseView; 
} 

@property (nonatomic,retain) UIView* baseView; 

您知道演習。

相關問題