无法触发Segue手动加载VC - self.navigationController是Nil?

时间:2014-07-11 09:36:12

标签: ios objective-c uiviewcontroller segue

修改

经过一些深入研究后,很明显,如果我在快速应用程序切换到/从Safari后尝试从视图控制器类调用segue,我会得到一个异常,说明segue不存在。它绝对是在屏幕上加载的VC中,我检查了拼写并且它100%正确。

更奇怪的是,如果我从IBAction调用完全相同的方法,它可以正常工作。

这是我正在使用的代码:

-(void)goToPhotoPage{

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

}

如果我在应用程序恢复时调用此方法,我会得到一个例外,但是如果我从这个IBAction调用它,附加到UIButton,没有做任何不同的事情,segue按预期工作,没有例外。

- (IBAction)twitterToPhoto:(id)sender
{
    [self goToPhotoPage];
}

Arrrrgrghrhhgrgrg!



原始问题

我正在开发一个将用户照片上传到Facebook / Twitter的iPad应用程序。

作为整个过程的一部分,我必须跳转到Safari为Twitter做OAuth,然后应用程序通过特定的URL跳回来获取令牌e.t.c

但由于某些原因,当应用程序重新唤醒时,我无法触发任何segue或手动加载任何VC。

我的AppDelegae中有一个成功块,当上传完成后调用它,调用VC中的方法来关闭微调器并转到成功视图。

旋转器按预期停止,但无论我尝试什么,我都无法让segue工作。

所有东西都被连接起来,segue有一个ID,我在控制台中没有任何错误或异常,只是没有任何反应。在这一点之后,我可以获得代码触发segue工作的唯一方法是使用用户触发器连接到UIButton,在完成后再开始工作。

以下是我的AppDelegate中带回调的代码:

- (void)postToTwitter:(NSString*)postText image:(UIImage*)image
{    
    NSData *data = UIImageJPEGRepresentation(image, 0.5);

    [_twitter postStatusUpdate:postText
                mediaDataArray:@[data]
             possiblySensitive:nil
             inReplyToStatusID:nil
                      latitude:nil
                     longitude:nil
                       placeID:nil
            displayCoordinates:nil
           uploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

           } successBlock:^(NSDictionary *status) {

               ViewController * vc = [[ViewController alloc]init];
               [vc triggerSuccess];

           } errorBlock:^(NSError *error) {

               ViewController * vc = [[ViewController alloc]init];
               [vc uploadFail:error];

           }];
}
VC中的

我尝试了以下内容:

- (void)triggerSuccess
{    
    [self segueToSuccess];
}

- (void)segueToSuccess
{    
    [self hideMessage];
    [self.navigationController performSegueWithIdentifier:@"approveSegue" sender:self];    
}

以及:

- (void)triggerSuccess
{    
    [self segueToSuccess];
}

- (void)segueToSuccess
{    
    [self hideMessage];
    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"done"];
    [self.navigationController pushViewController:controller animated:YES];
}

在绝望的时刻,我甚至尝试过:

- (void)triggerSuccess
{    
    [self segueToSuccess];
}

- (void)segueToSuccess
{    
    [self hideMessage];
    //[self.navigationController performSegueWithIdentifier:@"approveSegue" sender:self];
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popToRootViewControllerAnimated:NO];
    [self performSelector:@selector(segueToSuccessPart2) withObject:nil afterDelay:0.25];
}

- (void)segueToSuccessPart2
{    
    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"done"];
    [self.navigationController pushViewController:controller animated:YES];
}

我显然错过了一些东西,而且我猜测应该进入后台并且VC正在卸载"从记忆,需要重新安置,但我不知道从哪里开始。

任何帮助都会非常感激,因为我要把电脑扔出窗外。 。

由于

加雷

修改1

在进行了一些更多的故障排除之后,似乎在VC中的这次调用期间:

[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:NO];

self.navigationController是nil。 。

我猜这可能是我所看到的问题的原因?

修改2

正如Kelin所建议的,我正在努力确保我保留导航控制器。

我在AppDelegte中完成了以下操作:

@property (nonatomic, strong) UINavigationController *navController;

@synthesize navController;

但是当我尝试使用下面的代码从VC访问它时,它仍然是零。

AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
UINavigationController *navController = appDelegate.navController;

[self dismissViewControllerAnimated:NO completion:nil];
[navController popToRootViewControllerAnimated:NO];

编辑3

我还将其添加到AppDelegate,这意味着导航控制器不再是零。但仍然没有任何反应。 。

if (!self.navController)
{
    self.navController = [[UINavigationController alloc]initWithNibName:@"Main" bundle:nil];
}

2 个答案:

答案 0 :(得分:2)

问题是您尝试使用以编程方式创建的View Controller从故事板执行segue。

您使用-performSegueWithIdentifier: sender:完全错误。在这种方法"发件人"是一个按钮,或任何其他控件,其动作初始化segue,但不是要推动的视图控制器。

通常用故事板创建segue。但如果您确实需要手动创建它,请致电:

 -[UIStoryboardSegue initWithIdentifier:source:destination:]

其中源和目标是视图控制器。

阅读本文:https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html 或者这(关于自定义segues):

答案 1 :(得分:0)

你不能同时解雇和弹出,你必须弹出或关闭视图控制器和

每个ViewController都有它自己的NavigationController,它默认指向您添加到AppDelegate的导航控制器。

因此,您也可以通过self.navigationController(编辑3)

访问它
  

还要检查是否已初始化导航控制器   的AppDelegate

self.navController = [[UINavigationController alloc] initWithRootViewController:firstVC];