【问题标题】:RootViewController - iPhoneRootViewController - iPhone
【发布时间】:2011-08-02 13:03:05
【问题描述】:

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

场景:

我有 3 个视图

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

两个子视图都必须处于全屏模式,因此不能使用标签栏或导航栏。

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

查询

我已成功显示 SubView 1,当用户点击 DONE 时,我可以将其卸载。但是我没有明白如何从 Sub View 1 通知 RootViewController Sub View 1 已卸载,现在应该加载 Sub View 2?

提前致谢

Paras Mendiratta

【问题讨论】:

  • 如果你发布一些代码会很好......

标签: iphone


【解决方案1】:

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

【讨论】:

  • 这是一个很好的解决方案,他实际上也可以使用委托模式。 +1 虽然我没有想到这个解决方案。
  • 其实我对UINavigationController和Object-C中的委托模式也不是很清楚,所以我想去委托模式的基础教程。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    相反,您可以这样做:

    1. 在应用程序启动时将CricketViewController 设置为根视图控制器。
    2. 立即让CricketViewController 将“SportsViewController”呈现为模态视图控制器,没有动画。
    3. 就用户而言,运动视图控制器是起点。
    4. 点击完成按钮关闭模态视图控制器,让用户产生进入下一个视图的错觉。

    【讨论】:

      【解决方案4】:

      view1 = [[View1 alloc] initWithNibName:@"View1" bundle:nil]; //创建第一个视图 UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1]; navigationController1.navigationBar.tintColor =[UIColor blackColor]; view1 = 导航控制器1;
      [窗口添加子视图:view1.view]; [窗口 makeKeyAndVisible];

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-26
          • 2015-01-03
          • 1970-01-01
          • 1970-01-01
          • 2012-05-10
          • 2012-11-05
          相关资源
          最近更新 更多