ios 解雇视图控制器动画:完成:推送到另一个视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11390612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dismissViewControllerAnimated:completion: Push to another ViewController
提问by 278204
I'm just wondering if it's possible to push to a ViewController right after I dismissed one.
我只是想知道是否有可能在我解雇一个 ViewController 后立即推送到一个 ViewController。
I've been trying with this:
我一直在尝试这样做:
-(void)dismiss{
//send information to database here
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Dismiss completed");
[self pushtoSingle:post_id];
}];
}
-(void)pushtoSingle:(int)post_id{
Single1ViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SingleView"];
svc.post_id = post_id;
svc.page = 998;
[self.navigationController pushViewController:svc animated:YES];
}
And this:
和这个:
-(void)dismiss{
//send information to database here
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Dismiss completed");
Single1ViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SingleView"];
svc.post_id = post_id;
svc.page = 998;
[self.navigationController pushViewController:svc animated:YES];
}];
}
But with no success. The view is successfully is dismissed but the push is never initialized.. Is there another known way around the problem?
但没有成功。视图成功被解除,但推送从未初始化.. 是否有另一种解决问题的已知方法?
回答by 278204
Yep, I figured it out. I simply posted a notification after I dismissed the ViewController B to A and then received the notification in A and pushed to C..
是的,我想通了。我只是在将 ViewController B 解雇到 A 后发布了一个通知,然后在 A 中收到通知并推送到 C..
Dismissing in B:
在 B 中解雇:
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushToSingle" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:post_id1] forKey:@"post_id"]];
}];
Receiving in A:
在 A 接收:
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToSingle:) name:@"pushToSingle" object:nil];
}
-(void)pushToSingle:(NSNotification *)notis{
NSDictionary *dict = notis.userInfo;
int post_id = [[dict objectForKey:@"post_id"] intValue];
NSLog(@"pushing to single");
Single1ViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SingleView"];
svc.post_id = post_id;
svc.page = 998;
[self.navigationController pushViewController:svc animated:YES];
}
Thanks, Hymany Boy!
谢谢,杰基男孩!
回答by Rui Peres
Take out the animation. Like this:
取出动画。像这样:
[self dismissViewControllerAnimated:NO completion:^{
NSLog(@"Dismiss completed");
[self pushtoSingle:post_id];
}];
I had cases where multiple animations would create strange behaviors.
我遇到过多个动画会产生奇怪行为的情况。
Edit 1.0:
编辑 1.0:
Try this:
尝试这个:
[self performSelector:@selector(pushtoSingle:) withObject:post_id afterDelay:0.3];
Edit 2.0:
编辑 2.0:
Just notice now... You are dismissing your UIViewController
and you are trying to push a new one from your just dismissed UIViewController
. In theory, here:
现在请注意...您正在解雇您的UIViewController
并且您正试图从您刚刚解雇的UIViewController
. 理论上,这里:
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Dismiss completed");
[self pushtoSingle:post_id];
}];
On your completion block, self
will be nil, because you just dismiss it. Push the new UIViewController
from the current UIViewController
. Example, if your current UIViewController
is B:
在您的完成块上,self
将为零,因为您只是将其关闭。UIViewController
从当前推新UIViewController
。例如,如果您的电流UIViewController
是 B:
A -> B
After dismiss:
解散后:
A
Push the new one:
推新的:
A -> C
回答by Nathan Kellert
This is more of just adding to the answer provide above presented by robot6.
这更多的是添加到robot6上面提供的答案。
If you are going to be adding the new view as a child view you should really consider also adding the following to the parent view as well.
如果您打算将新视图添加为子视图,您真的应该考虑还将以下内容添加到父视图中。
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
In my case I was launching a AlertView based off of the int passed back through the notifications NSDictionary.Then I was presenting the same child view again after accepting the AlertView.
在我的情况下,我基于通过通知 NSDictionary 传回的 int 启动 AlertView。然后我在接受 AlertView 后再次呈现相同的子视图。
-(void)pushToSingle:(NSNotification *)notis{
NSDictionary *dict = notis.userInfo;
int post_id = [[dict objectForKey:@"post_id"] intValue];
NSLog(@"%d", post_id);
if (post_id == 1) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Sign Up"
message:@"Are you sure you want to cancel?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles: @"Accept", nil];
alert.tag = 1;
post_id = 4;
[alert show];
}
}
Heres what was happening.
继承人发生了什么。
So, through the above answer I learned that basically anything you do in view 1 will go again in your child view as though it was still on your parent view as you never removed the NSNotification observer and the observer is still active.
所以,通过上面的答案我了解到,基本上你在视图 1 中所做的任何事情都会在你的子视图中再次出现,就好像它仍然在你的父视图中一样,因为你从未删除 NSNotification 观察者并且观察者仍然处于活动状态。
Now this may not cause problems for everyone but it will eat up system resources if you dont really need it as ARC wont release the observer until the parent view is no longer active or unless you tell it to do so in your viewDidDisapper
现在这可能不会对每个人造成问题,但如果您真的不需要它,它会消耗系统资源,因为 ARC 不会释放观察者,直到父视图不再活动,或者除非您在您的视图中告诉它这样做 viewDidDisapper
回答by dadachi
Category:
类别:
class Category {
var name: String = ""
}
FirstViewController:
第一个视图控制器:
class FirstViewController: UIViewController {
func addNewCategory() {
let category = Category()
let secondViewController = SecondViewController(category: category)
secondViewController.didCreateCategory = { (category: Category) in
let thirdViewController = ThirdViewController(category: category)
self.navigationController?.pushViewController(thirdViewController, animated: true)
}
let secondNavigationController = UINavigationController(rootViewController: secondViewController)
presentViewController(secondNavigationController, animated: true, completion: nil)
}
}
SecondViewController:
第二视图控制器:
class SecondViewController: UIViewController {
@IBOutlet weak var categoryNameTextField: UITextField!
var didCreateCategory: ((category: Category) -> ())?
var category: Category
init(category: Category) {
self.category = category
super.init(nibName: nil, bundle: nil)
}
convenience required init(coder aDecoder: NSCoder) {
self.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add,
target: self, action: "createCategory")
}
func createCategory() {
dismissViewControllerAnimated(true, completion: {
self.category.name = self.categoryNameTextField.text
self.didCreateCategory!(category: self.category)
})
}
}
ThirdViewController:
第三个视图控制器:
class ThirdViewController: UIViewController {
var category: Category
init(category: Category) {
self.category = category
super.init(nibName: nil, bundle: nil)
}
convenience required init(coder aDecoder: NSCoder) {
self.init(coder: aDecoder)
}
}