【问题标题】:How to move to another view without using a button如何在不使用按钮的情况下移动到另一个视图
【发布时间】:2012-10-22 10:50:37
【问题描述】:

我的第一个视图显示图像和操作指示器,而 Web 服务器和数据库功能在该背景下运行。我希望应用程序在功能完成后转到我的选项卡视图。我该怎么做呢?

这是视图的样子。

我尝试过的:

TabBarViewController *tab = [[TabBarViewController alloc]init];
[self presentViewController:tab animated:NO completion:nil];

[self performSegueWithIdentifier:@"first" sender:self];

请您帮助我了解如何执行此操作。我花了很多时间在谷歌上搜索这个问题,但不知道该怎么做。

谢谢

编辑:添加Code

【问题讨论】:

  • 第一次你就想做这个吧?
  • @ParasJoshi 是的,我希望在用户第一次加载应用程序时以及之后每次启动应用程序时都发生这种情况,以便在需要时更新数据库。
  • 好的,我用 UIImageView 添加一个逻辑.. 看我的代码...
  • 如果视图控制器在故事板中,那么您不能简单地分配初始化它。您必须使用 segue 方法的 -destinationViewController 或 UIStoryboard 方法的控制器标识符来获取它。而且您还需要覆盖 -prepareForSegue。

标签: iphone objective-c ios xcode ios6


【解决方案1】:

由于您正在下载数据,因此您可以在下载请求完成时接到电话 像下面的方法

- (void)requestFinished:(ASIHTTPRequest *)request

通过这种方法,您可以很好地展示您的TabBarViewController

【讨论】:

    【解决方案2】:

    您可以使用 GCD 来实现这一点。

    例如,在您触发下载的firstViewController 中,您可以执行以下操作:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [model downloadData]; dispatch_sync(dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self]; }); });

    我假设您的 downloadData 方法是同步的,如果不是,您可以在模型中使用通知。检索到数据后,您可以从NSNotificationCenterfirstViewController 中注册postNamedNotification 以获取通知,并在收到通知后致电performSegueWithIdentifier

    【讨论】:

    • 我现在使用 GCD 来做所有的数据库和网络服务。所以它们在活动指示器动画时在后台运行。
    • 所以最好的方法是在你的 GCD 任务中 postNamedNotification 并在你的 viewController 中注册它。然后从viewController的通知调用performSegueWithIdentifier触发的方法@
    • 这成功了!需要添加 [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];在 dispatch_async 里面!谢谢。
    • 太好了。您可以考虑在主线程中调用performSegueWithIdentifier。例如dispatch_sync(dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier ...
    • 如果我在主线程中调用它,即使视图发生变化,我的其他函数是否仍会运行?还是在它们运行之前视图不会改变?
    【解决方案3】:

    您可以为此声明一个单独的方法。

    当你的函数完成时调用下面的方法。

    [self performSelector:@selector(gotonext:) withObject:nil afterDelay:4.0];
    
    -(void)gotonext;
    {
    TabBarViewController *tab = [[TabBarViewController alloc]init];
    [self presentViewController:tab animated:NO completion:nil];
    }
    

    【讨论】:

    • 是的,您可以根据需要修改它。
    • 唯一的问题是我不知道运行这些函数需要多长时间。下载所有内容可能需要 10 秒,另一次可能需要 1 秒。我只是要在其他方法完成后调用该方法。我遇到的问题是让它显示下一个视图。
    【解决方案4】:

    在开始时拍摄一张 splashView 图像,如下所示...

        @interface AppDelegate : UIResponder <UIApplicationDelegate>{
            UIImageView *splashView;
        }
        @property (nonatomic, retain) UIImageView *splashView;
        @end
    

    在 AppDelegate.m 文件中...

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
        {
            splashView = [[UIImageView alloc] initWithFrame:iphoneFrame];
            splashView.image = [UIImage imageNamed:@"Default"];
            [self.window addSubview:splashView];
            [self.window makeKeyAndVisible];
    
            UIViewController *viewController1 = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
                    UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
                    navviewController1.title = @"FirstTitle";
            //        navviewController1.navigationBarHidden=YES;
    
            UIViewController *viewController2 = [[[yourviewController2 alloc] initWithNibName:@"yourviewController2" bundle:nil] autorelease];
                    UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
            //        navviewController2.navigationBarHidden=YES;
                    navviewController2.title = @"SecondTitle";
    
            UIViewController *viewController3 = [[[yourviewController3 alloc] initWithNibName:@"yourviewController2" bundle:nil] autorelease];
                    UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
            //        navviewController3.navigationBarHidden=YES;
                    navviewController3.title = @"ThirdTitle";
    
                   //..... and so on depend on your requirement 
    
            self.tabBarController = [[[UITabBarController alloc] init] autorelease];
            self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2 , navviewController3 ,nil];
            [self performSelector:@selector(loadViewIphone) withObject:nil afterDelay:2.0];//**add this line at your all data are loading completed**
        } 
        else 
        {
            splashView = [[UIImageView alloc] initWithFrame:ipadFrame];
            splashView.image = [UIImage imageNamed:@"Default_iPad"];
            [self.window addSubview:splashView];
            [self.window makeKeyAndVisible];
            [self performSelector:@selector(loadViewIpad) withObject:nil afterDelay:2.0];
        }
    
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    -(void)loadViewIphone 
    {
        [splashView removeFromSuperview];
    
        [self.window addSubview:tabBarController.view];
        [self.window makeKeyAndVisible];   
    
        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];   
        [animation setType:kCATransitionFromBottom];
        [animation setDuration:1.0];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                      kCAMediaTimingFunctionEaseInEaseOut]];
        [[self.window layer] addAnimation:animation forKey:@"transitionViewAnimation"];
    
    
        [self.window makeKeyAndVisible];
    }
    

    希望对你有帮助……

    【讨论】:

      【解决方案5】:

      嗯...如果你把

       TabBarViewController *tab = [[TabBarViewController alloc]init];
       [self presentViewController:tab animated:NO completion:nil];
      

      dispatch_async(dispatch_get_main_queue(), ^{
              //stop the loader once the database stuff has finished and get rid of the text
              [[self firstLoader]stopAnimating];
              self.downloadingLabel.text = @"";
      
              });
      

      更新:如果您想进行此同步

      dispatch_sync(a_queue, ^{ wait_for_me(); }); 然后展示你的 VC。

      【讨论】:

      • 当我这样做时,它会移动到底部带有 tabBar 的视图,但不显示任何按钮或 tabPages
      • 我需要把 [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];在 dispatch_async 里面!谢谢。
      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2018-04-26
      • 1970-01-01
      相关资源
      最近更新 更多