gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:02:50 24 4
gpt4 key购买 nike

编辑

经过更多的挖掘之后,很明显,如果我在快速应用程序切换到/从 Safari 后尝试从 View Controller 类调用 segue,我会得到一个异常,说明 segue 不存在。确实如此,在屏幕上加载的 VC 中,我检查了拼写,它 100% 正确。

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

这是我使用的代码:

-(void)goToPhotoPage{

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

}

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

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

啊啊啊啊啊啊啊啊啊!



原始问题

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

作为该过程的一部分,我必须跳转到 Safari 为 Twitter 执行 OAuth,然后该应用程序通过特定 URL 跳回并获取 token 等

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

我的 AppDelegae 中有一个成功 block ,它在上传完成时被调用,它调用 VC 中的方法来关闭微调器并转到成功 View 。

微调器按预期停止,但无论我尝试什么,我都无法让 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 的建议,我正在努力确保保留导航 Controller 。

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

@property (nonatomic, strong) UINavigationController *navController;

@synthesize navController;

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

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

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

编辑3

我还将其添加到 AppDelegate,这意味着 Navigation Controller 不再是 nil。但仍然没有任何反应。 . .

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

最佳答案

问题是您尝试使用以编程方式创建的 View Controller 从 Storyboard执行转场。

你使用 -performSegueWithIdentifier: sender: 完全错误。在此方法中,“sender”是一个按钮,或任何其他控件,其操作初始化 segue,但不是要推送的 View Controller 。

通常 segue 是用 Storyboard创建的。但是,如果您确实需要手动创建它,请调用:

 -[UIStoryboardSegue initWithIdentifier:source:destination:]

源和目标是 View Controller 。

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

关于ios - 无法触发 Segue 手动加载 VC - self.navigationController 为 Nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com