我对iPhone编程很陌生,并试图掌握RootViewController的概念。
场景:
我有三种观点
两个子视图都必须在FullScreenMode中,因此不能使用选项卡条或导航栏。
首先,加载Sub View 1,其中包含一些内容和一个DONE按钮。一旦用户按下“完成”按钮,子视图1就必须被卸载,RootViewController应该加载Sub 2。
查询
我已经成功地显示了SubView 1,当用户点击完成之后,我可以卸载它。但是,我没有得到如何通知子视图1的RootViewController子视图1已经卸载,现在它应该加载子视图2?
提前谢谢
Paras Mendiratta
发布于 2011-08-02 21:36:17
下面是我尝试使用委托模式的代码。
问题是子视图1 (videoPlayer)无法调用委托方法。:(
ViewSwitcher.h -根控制器
@class VideoPlayer; //Sub View 1
@class LandingPage; //Sub View 2
@protocol viewSwitcherDelegate
-(void)notifyViewSwitcher;
@end
@interface ViewSwitcher : UIViewController
{
id <viewSwitcherDelegate> delegate;
}
@property (nonatomic, retain) VideoPlayer *videoPlayer;
@property (nonatomic, retain) LandingPage *landingPage;
@property(assign) id <viewSwitcherDelegate> delegate;
-(void)loadSecondView;
-(void)delegateSetInSubView;
@endViewSwitcher.m -实现
@synthesize videoPlayer;
@synthesize landingPage;
//@synthesize delegate;
// 1.4 -> Declare the delegate constructor
- (id <viewSwitcherDelegate>)delegate
{
return delegate;
}
// 1.5 -> Declare the setDelegate method
- (void)setDelegate:(id <viewSwitcherDelegate>)v
{
delegate = v;
}
- (void)viewDidLoad
{
VideoPlayer *videoController = [[VideoPlayer alloc] initWithNibName:@"VideoPlayer" bundle:nil];
self.videoPlayer = videoController;
[self.view insertSubview:videoController.view atIndex:0];
[videoController release];
[super viewDidLoad];
}
-(void)loadSecondView
{
NSLog(@"Call for loading 2nd View");
}视频Player.h- SubView 1(电影播放器)
@interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
ViewSwitcher *viewSwitcher;
MPMoviePlayerController *videoController;
}
@endVideoPlayer.m -实现
-(void)notifyViewSwitcher
{
NSLog(@"notifyViewSwitcher called.");
//Attempted to call the loadSecondView of ViewSwitcher我试着调用委托的方法,但是在日志中没有打印.
[viewSwitcher loadSecondView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Setting the delegate
viewSwitcher.delegate = self;
return self;
}
- (void)viewDidLoad
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"bumper.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
videoController.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:videoController.view];
videoController.view.frame = CGRectMake(0, 0, 480, 320);
videoController.fullscreen = YES;
// Remove the status bar from this view.
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:UIStatusBarAnimationFade];
// TODO: This observer needs to be removed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateChange:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
// Play the video.
[videoController play];
[super viewDidLoad];
}
// Receives notification once movie is finished.
-(void)playbackStateChange:(NSNotification*)notification
{
// TODO: Switch the view.
NSLog(@"Notification = %@", notification);
[self notifyViewSwitcher];
}这里是日志:-
2011-08-03 02:44:47.333 My Video Player[24016:207] Notification = NSConcreteNotification 0x5768280 {name = MPMoviePlayerPlaybackDidFinishNotification; object = <MPMoviePlayerController: 0x5740940>; userInfo = {
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 0;
}}
2011-08-03 02:44:47.337 My Video Player[24016:207] notifyViewSwitcher called.发布于 2011-08-02 13:07:47
我认为这里最简单的解决方案是使用UINavigationController并隐藏导航栏。您可以使用-setNavigationBarHidden:animated:隐藏(或显示)导航条。
发布于 2011-08-02 13:10:31
一种方法是实现像- (void)loadSecondView这样的方法,在卸载第一个视图时做所有您想做的事情。然后int -,您可以这样调用这个方法:[super loadSecondView];,并从superview中删除第一个视图。
https://stackoverflow.com/questions/6912515
复制相似问题