以同一方法处理和运行多个 ViewController
Posted
技术标签:
【中文标题】以同一方法处理和运行多个 ViewController【英文标题】:Handling and running multiple ViewControllers in the same method 【发布时间】:2014-07-29 09:22:24 【问题描述】:我有一个 MainViewController,它有一个按钮 init。在按钮上单击图片开始动画,然后我创建一个不同 ViewController 的新实例,该实例运行 Web 请求并提取一些信息并将其推送到前面。我希望在中间视图控制器运行时图像是动画的。最后,一旦 MiddleViewController 完成,在同一个按钮方法中,我切换到第三个 ViewController。正如预期的那样,我收到以下错误:
Warning: Attempt to present <ThirdViewController: 0x962b470> on <MainViewController: 0x986c200> while a presentation is in progress!
Unbalanced calls to begin/end appearance transitions
我该如何解决这个问题?我宁愿不移除中间的 ViewController 并将其代码集成到 MainViewController 中。
-(void)singleTapping:(UIGestureRecognizer *)recognizer
UIImage *rot1 = [UIImage imageNamed:@"pic1.png"];
UIImage *rot2 = [UIImage imageNamed:@"pic2.png"];
UIImage *rot3 = [UIImage imageNamed:@"pic3.png"];
UIImage *rot4 = [UIImage imageNamed:@"pic4.png"];
self.scanclickedimage.animationImages = [[NSArray alloc] initWithObjects:rot1, rot2, rot3, rot4, nil];
self.scanclickedimage.animationDuration = 0.6f;
self.scanclickedimage.animationRepeatCount = HUGE_VALF;
[self.scanclickedimage startAnimating];
if (nextString == NULL || [nextString isEqualToString:@""])
NSLog(@"Initializing MiddleViewController");
MiddleViewController * middleViewController = [[MiddleViewController alloc] init];
middleViewController.delegate = self;
[self presentViewController:middleViewController animated:YES completion:nil];
else
ProductWebRequest *request = [[ProductWebRequest alloc] init];
[request sendWebRequest:nextString];
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:YES completion:nil];
【问题讨论】:
【参考方案1】:有时,当您同时呈现/推送超过 2 个视图控制器(或非常快)并且“动画”选项设置为“是”时,会出现此错误。
尝试像这样将动画设置为“NO”:
[self presentViewController:middleViewController animated:NO completion:nil];
或者做这样的事情:
[self presentViewController:middleViewController animated:YES completion:
[self presentViewController:vc animated:YES completion:nil];
];
您必须更新您的代码才能执行此操作。尝试将所有内容放在一个单独的方法中,如下所示:
-(void)singleTapping:(UIGestureRecognizer *)recognizer
UIImage *rot1 = [UIImage imageNamed:@"pic1.png"];
UIImage *rot2 = [UIImage imageNamed:@"pic2.png"];
UIImage *rot3 = [UIImage imageNamed:@"pic3.png"];
UIImage *rot4 = [UIImage imageNamed:@"pic4.png"];
self.scanclickedimage.animationImages = [[NSArray alloc] initWithObjects:rot1, rot2, rot3, rot4, nil];
self.scanclickedimage.animationDuration = 0.6f;
self.scanclickedimage.animationRepeatCount = HUGE_VALF;
[self.scanclickedimage startAnimating];
if (nextString == NULL || [nextString isEqualToString:@""])
NSLog(@"Initializing MiddleViewController");
MiddleViewController * middleViewController = [[MiddleViewController alloc] init];
middleViewController.delegate = self;
[self presentViewController:middleViewController animated:YES completion:^
[self launchTest];
];
else
ProductWebRequest *request = [[ProductWebRequest alloc] init];
[request sendWebRequest:nextString];
[self launchTest];
- (void)launchTest
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:YES completion:nil];
【讨论】:
感谢您的回复。不幸的是,我已经尝试过了,它并没有解决问题 第二部分代码提供了“Expected Expression”的错误 “预期表达式”错误或“开始/结束外观转换的不平衡调用”? 不平衡的。我更新了错误。也许是动画图像的原因?我怎样才能将其设置为仅在 ProductWebRequest 运行时设置动画? 我注意到你使用 Storyboard... 你应该在 Storyboard 中使用 Segue 来展示你的 middleViewController。只需连接 segue,选择您想要的礼物类型,ios 就可以了。谢谢,在 MiddleViewController 的 viewDidLoad 或 viewDidAppear 方法中,以编程方式呈现另一个。以上是关于以同一方法处理和运行多个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章