编辑
经过进一步的挖掘,很明显,如果我在应用程序快速切换到Safari后尝试从视图控制器类调用segue,我会得到一个异常,说明segue不存在。确实如此,在屏幕上加载的VC中,我已经检查了拼写,它是100%正确的。
更奇怪的是,如果我从IBAction调用完全相同的方法,它会很好地工作。
这是我正在使用的代码:
-(void)goToPhotoPage{
[self performSegueWithIdentifier:@"twitterAuthComplete" sender:self];
}
如果我在应用程序恢复时调用它,我会得到一个异常,但是,如果我从这个附加到UIButton的IBAction调用它,不做任何不同的事情,segue就会按预期工作,没有异常。
- (IBAction)twitterToPhoto:(id)sender
{
[self goToPhotoPage];
}
Arrrrgrghrhhgrgrg!
原始问题
我正在开发一个iPad应用程序,可以将用户照片上传到Facebook/Twitter。
作为过程的一部分,我必须跳到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从内存中"卸载"有关,需要重新安装,但我不确定从哪里开始。
如果有任何帮助,我将不胜感激,因为我正要把电脑扔出窗外。
感谢
Gareth
编辑1
在进行了更多的故障排除之后,在VC:中的调用过程中
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:NO];
self.navigationController为零。
我猜这可能是我看到的问题的原因吧?
编辑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];
}
问题是您试图使用程序创建的视图控制器从情节提要执行segue。
您使用-performSegueWithIdentifier: sender:
完全错误。在这种方法中,"发送器"是一个按钮,或任何其他动作初始化segue的控件,但不是要按下的视图控制器。
通常segue是用故事板创建的。但如果你真的需要手动创建,请致电:
-[UIStoryboardSegue initWithIdentifier:source:destination:]
其中源和目的地是视图控制器。
阅读以下内容:https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html或者这个(关于自定义分段(:
不能同时关闭和弹出,也不能同时关闭视图控制器和
每个ViewController都有自己的NavigationController,它是您添加到AppDelegate的导航控制器的默认指向点。
因此,您也可以通过self.navigationController
(编辑3(访问它
同时检查是否已在中初始化导航控制器AppDelegate
self.navController = [[UINavigationController alloc] initWithRootViewController:firstVC];