代码之家  ›  专栏  ›  技术社区  ›  Keith Adler

在DidFinishLaunchingWithOptions中将视图显示为初始屏幕

  •  2
  • Keith Adler  · 技术社区  · 15 年前

    我有一个标签栏应用程序,我想在didfindishingswithoptions方法加载标签栏控制器后,简单地显示一个视图(初始屏幕)。为什么这么难?请演示如何加载并在下面显示名为splashview.xib的XIB文件,然后显示它:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
        // Override point for customization after application launch.
    
        // Add the tab bar controller's view to the window and display.
        [window addSubview:tabBarController.view];
        [window makeKeyAndVisible];
    
        // Load up and show splash screen Xib here
    
        return YES;
    }
    
    3 回复  |  直到 15 年前
        1
  •  4
  •   Ben Scheirman    15 年前

    我要说的第一件事是防溅屏在HIG中是特别不受欢迎的。尤其是那些只会让用户等着看他们不在乎的标志的。

    现在,rant已经过时了,我假设您可能已经有了一些加载过程,您希望在标签显示之前发生。

    在这种情况下,我不会在mainwindow.xib中加载选项卡。相反,我启动执行加载的单一视图(使用XIB)。原因是:在你能看到你的闪屏之前,你需要支付所有这些视图的加载费用。

    在加载数据的情况下,有时这些选项卡依赖于正在加载的数据,因此等待加载选项卡栏控制器更有意义。

    应用程序代理最终看起来像这样:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        [window makeKeyAndVisible];
        [window addSubview:splashController.view];  //this assumes MainWindow.xib defines your splash screen, otherwise....
    
        UIViewController *splashController = [[SplashController alloc] initWithNibName:@"SplashController" bundle:nil];
        splashController.delegate = self;
        [window addSubview:splashController.view];
    
        //hang on to it in an ivar, remember to release in the dealloc
    }
    

    然后在启动屏幕控制器中,当我完成加载后,我执行以下操作:

    -(void)doneLoading {
        [self.delegate performSelector:@selector(splashScreenDidFinishLoading)];
    }
    

    当然 self.delegate 不存在,可以这样简单地添加:

    //header
    @property (nonatomic, assign) id delegate;
    
    //implementation
    @synthesize delegate;
    

    然后确保并在应用程序委托上实现该方法:

    -(void)splashScreenDidFinishLoading {
         //load up tab bar from nib & display on window
         //dispose of splash screen controller
    }
    

    我已经在一些应用程序中使用了这种模式,它很简单并且工作得很好。你也可以选择在上面的方法中做一个很好的过渡动画。

    希望这有帮助。

        2
  •  1
  •   Costique    15 年前

    在应用程序代理的头文件中:

    @interface AppDelegate {
        ...
        IBOutlet UIView *splash; // add this line
    }
    

    在ib中打开splashview.xib,将文件所有者的类设置为应用程序委托的类,连接splash outlet。添加此项以显示初始视图:

    [[NSBundle mainBundle] loadNibNamed: @"SplashView" owner: self options: nil];
    [window addSubview: splash];
    

    您可能也希望隐藏启动视图:

    [splash removeFromSuperview];
    [splash release];
    splash = nil;
    

    您可以使用uiview动画块淡出初始视图以获得额外的酷效果。也就是说,防溅屏 邪恶的 .

    我认为应用程序代表确实是一个更好的地方,这样的东西。

        3
  •  1
  •   diatrevolo    15 年前

    我会做如下的事情:

        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Splash.png"]];
        [imageView setCenter:CGPointMake(240, 160)];
    
        self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [self.view addSubview:imageView];
    
        [imageView retain];
    
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self //display for 3 secs
         selector:@selector(continueLoadingWhatever:)
         userInfo:nil
         repeats:NO];
    

    然后。。。

    - (void)continueLoadingWhatever:(id)sender {
        //do whatever comes after here
    }
    

    我可能不会在应用程序委托中这样做,而是在根视图控制器中。您不应该直接向窗口添加任何不必要的内容,尤其是当窗口包含交互时(我知道这不是)。