Ios 无法触发Segue手动加载VC-self.navigationController为零?

Ios 无法触发Segue手动加载VC-self.navigationController为零?,ios,objective-c,uiviewcontroller,segue,Ios,Objective C,Uiviewcontroller,Segue,编辑 经过进一步深入研究,很明显,如果我在快速切换到Safari或从Safari调用应用程序后尝试从view controller类调用segue,我会收到一个异常,表明segue不存在。确实是这样,在屏幕上加载的VC中,我检查了拼写,它是100%正确的 更奇怪的是,如果我从IBAction调用完全相同的方法,它就可以正常工作 以下是我正在使用的代码: -(void)goToPhotoPage{ [self performSegueWithIdentifier:@"twitterAut

编辑

经过进一步深入研究,很明显,如果我在快速切换到Safari或从Safari调用应用程序后尝试从view controller类调用segue,我会收到一个异常,表明segue不存在。确实是这样,在屏幕上加载的VC中,我检查了拼写,它是100%正确的

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

以下是我正在使用的代码:

-(void)goToPhotoPage{

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

}
如果我在应用程序恢复时调用它,我会得到一个异常,但是如果我从这个iAction调用它,附加到一个UIButton,没有做任何不同的事情,segue会按照预期工作,没有异常

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



原始问题

我正在开发一款iPad应用程序,可以将用户照片上传到Facebook/Twitter

作为过程的一部分,我必须跳转到Safari为twitter创建OAuth,然后应用程序通过一个特定的URL跳回,并获得令牌e.t.c

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

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

旋转器按预期停止,但无论我如何尝试,都无法使该序列工作

一切都连接好了,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为零

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

编辑2

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

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

@property (nonatomic, strong) UINavigationController *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];
}

问题是您试图使用以编程方式创建的视图控制器从情节提要执行分段

您使用的
-performsguewithidentifier:sender:
完全错误。在这种方法中,“发送器”是一个按钮,或任何其他操作初始化segue的控件,但不是要按下的视图控制器

通常,segue是用故事板创建的。但如果确实需要手动创建,请调用:

 -[UIStoryboardSegue initWithIdentifier:source:destination:]
其中,源和目标是视图控制器

请阅读以下内容:
或者这(关于自定义序列):

您不能同时执行“取消”和“同时弹出”,或者必须弹出或取消视图控制器和

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

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

还要检查您是否在中初始化了导航控制器 AppDelegate


嗨,科林,谢谢你的迅速回复。为了清楚起见,我已经在故事板中手动创建了一个从VC到目标VC的序列,并给它一个标识符。有趣的是,我刚刚做了更多的故障排除,当我尝试跳转到rootVC self.navigationController时,它似乎为零,我猜这是许多问题的根源?可能是的,您是否在AppDelegate或其他地方保留对导航控制器的强引用?不,除了标准样板之外,我没有添加任何内容来保留对导航控制器的强引用。您需要在其他地方保留对导航控制器的强引用。在旧的样板中,AppDelegate中强烈引用了导航控制器。新项目使用故事板,没有这样的属性。嗨,科林,非常感谢你的帮助。因此,如果我将@property(强,非原子)UINavigationController*navigationController添加到我的VC类中;合成完成这项工作。或者我需要通过AppDelegate传递它,以便在卸载VC时不会删除它?谢谢
 -[UIStoryboardSegue initWithIdentifier:source:destination:]
self.navController = [[UINavigationController alloc] initWithRootViewController:firstVC];