开发者

RootViewController - iPhone

I am new to iPhone programming and trying to grasp the concept of RootViewController.

Scenario:

I have 3 views

  1. RootViewController - Parent View
  2. SportsViewController - Sub View 1
  3. CricketViewController - Sub View 2

Both the subview have to be in FullScreenMode, so tab-bar or navigation bar cannot be used.

At first, Sub View 1 is loaded which is having some content and a DONE button on it. Once user press DONE button then Sub View 1 has to be unloaded and RootViewController should load the Sub View 2.

Query

I have successfu开发者_如何学Clly displayed SubView 1 and when user taps on DONE then I can unload it. But I did n't get how should I notify the RootViewController from Sub View 1 that Sub View 1 has unloaded and now it should load the Sub View 2?

Thanks in Advance

Paras Mendiratta


I think the easiest solution here is to use UINavigationController and just hide the navigation bar. You can use -setNavigationBarHidden:animated: to hide (or show) the nav bar.


one way is to implement a method like - (void)loadSecondView which does all u want to do when first view is unloaded. and then int doneButtonClicked-method u call this method like this: [super loadSecondView]; and also remove first view from superview.


I assume that the screen with the "Done" button is some kind of login screen. You actually do not need all the view controllers you have defined to do what you want.

Instead you could do like this:

  1. At application launch set CricketViewController as the root view controller.
  2. Immediately let CricketViewController present `SportsViewController" as a modal view controller, no animation.
  3. As far as the user is concerned the sports view controller is the starting point.
  4. Dismiss the modal view controller on the Done button tap, giving the user the illusion of going to the next view.


view1 = [[View1 alloc] initWithNibName:@"View1" bundle:nil]; //Create the first view UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1]; navigationController1.navigationBar.tintColor =[UIColor blackColor]; view1 = navigationController1;
[window addSubview:view1.view]; [window makeKeyAndVisible];

This is general idea so please change according to your problem.


Here is my code where I tried to use delegate pattern.

The problem is the sub view 1 (videoPlayer) is not able to call the delegate methods. :(

ViewSwitcher.h - Root Controller

@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 - Implementation

@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 (Movie Player)

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

@end

VideoPlayer.m - implementation

-(void)notifyViewSwitcher
{
    NSLog(@"notifyViewSwitcher called.");
    //Attempted to call the loadSecondView of ViewSwitcher

I tried calling delegate's method but in log nothing is printed.

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

Here is the LOG:-

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜