RootViewController - iPhone

时间:2011-08-02 13:03:05

标签: iphone

我是iPhone编程的新手,并试图掌握RootViewController的概念。

情景:

我有3个观看次数

  1. RootViewController - 父视图
  2. SportsViewController - 子视图1
  3. CricketViewController - Sub View 2
  4. 子视图都必须在FullScreenMode中,因此不能使用标签栏或导航栏。

    首先,加载Sub View 1,其中包含一些内容和DONE按钮。 一旦用户按下DONE按钮,则必须卸载Sub View 1,RootViewController应加载Sub View 2。

    查询

    我已成功显示SubView 1,当用户点击DONE时,我可以卸载它。但是我没有得到如何从子视图1通知RootViewController,Sub View 1已经卸载,现在它应该加载Sub View 2?

    先谢谢

    Paras Mendiratta

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

一种方法是实现像- (void)loadSecondView这样的方法,它可以在卸载第一个视图时执行所有操作。然后int doneButtonClicked-method你这样调用这个方法:[super loadSecondView];并从superview中删除第一个视图。

答案 2 :(得分:0)

我认为带有“Done”按钮的屏幕是某种登录屏幕。实际上,您并不需要已定义的所有视图控制器来执行您想要的操作。

相反,你可以这样做:

  1. 在应用程序启动时将CricketViewController设置为根视图控制器。
  2. 立即让CricketViewController呈现`SportsViewController'作为模态视图控制器,没有动画。
  3. 就用户而言,体育视图控制器是起点。
  4. 点击 Done 按钮上的模态视图控制器,让用户感觉可以进入下一个视图。

答案 3 :(得分:0)

  

view1 = [[View1 alloc] initWithNibName:@“View1”bundle:nil]; //创建第一个视图   UINavigationController * navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];   navigationController1.navigationBar.tintColor = [UIColor blackColor];   view1 = navigationController1;
  [window addSubview:view1.view];   [window makeKeyAndVisible];

这是一般性的想法所以请根据您的问题进行更改。

答案 4 :(得分:0)

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

问题是子视图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;

@end

ViewSwitcher.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");
}

VideoPlayer.h - SubView 1(电影播放器​​)

@interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
    ViewSwitcher *viewSwitcher;
    MPMoviePlayerController *videoController;
}

@end

VideoPlayer.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.