首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RootViewController - iPhone

RootViewController - iPhone
EN

Stack Overflow用户
提问于 2011-08-02 13:03:05
回答 5查看 865关注 0票数 0

我对iPhone编程很陌生,并试图掌握RootViewController的概念。

场景:

我有三种观点

  1. RootViewController -父View
  2. SportsViewController -子视图1
  3. CricketViewController -子视图2

两个子视图都必须在FullScreenMode中,因此不能使用选项卡条或导航栏。

首先,加载Sub View 1,其中包含一些内容和一个DONE按钮。一旦用户按下“完成”按钮,子视图1就必须被卸载,RootViewController应该加载Sub 2。

查询

我已经成功地显示了SubView 1,当用户点击完成之后,我可以卸载它。但是,我没有得到如何通知子视图1的RootViewController子视图1已经卸载,现在它应该加载子视图2?

提前谢谢

Paras Mendiratta

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-08-02 21:36:17

下面是我尝试使用委托模式的代码。

问题是子视图1 (videoPlayer)无法调用委托方法。:(

ViewSwitcher.h -根控制器

代码语言:javascript
复制
@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;

@end

ViewSwitcher.m -实现

代码语言:javascript
复制
@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(电影播放器)

代码语言:javascript
复制
@interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
    ViewSwitcher *viewSwitcher;
    MPMoviePlayerController *videoController;
}

@end

VideoPlayer.m -实现

代码语言:javascript
复制
-(void)notifyViewSwitcher
{
    NSLog(@"notifyViewSwitcher called.");
    //Attempted to call the loadSecondView of ViewSwitcher

我试着调用委托的方法,但是在日志中没有打印.

代码语言:javascript
复制
    [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];
}

这里是日志:-

代码语言:javascript
复制
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.
票数 0
EN

Stack Overflow用户

发布于 2011-08-02 13:07:47

我认为这里最简单的解决方案是使用UINavigationController并隐藏导航栏。您可以使用-setNavigationBarHidden:animated:隐藏(或显示)导航条。

票数 2
EN

Stack Overflow用户

发布于 2011-08-02 13:10:31

一种方法是实现像- (void)loadSecondView这样的方法,在卸载第一个视图时做所有您想做的事情。然后int -,您可以这样调用这个方法:[super loadSecondView];,并从superview中删除第一个视图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6912515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档