dismissViewControllerAnimated:completion:推送到另一个ViewController

时间:2012-07-09 07:22:29

标签: objective-c ios

我只是想知道在我解雇之后是否可以推送到ViewController。

我一直在尝试这个:

     -(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];
}

而且:

    -(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];
                }];
}

但没有成功。视图成功被取消但推送从未初始化.. 还有其他已知方法来解决这个问题吗?

4 个答案:

答案 0 :(得分:28)

是的,我明白了。在我将ViewController B解除为A然后在A中收到通知并推送到C之后,我只是发布了一个通知。

解雇B:

[self dismissViewControllerAnimated:YES completion:^{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToSingle" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:post_id1] forKey:@"post_id"]];
}];

接收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];

}

谢谢,Jacky Boy!

答案 1 :(得分:2)

取出动画。像这样:

[self dismissViewControllerAnimated:NO completion:^{
                NSLog(@"Dismiss completed");
                [self pushtoSingle:post_id];
            }];

我遇到多个动画会产生奇怪行为的情况。

修改1.0:

试试这个:

[self performSelector:@selector(pushtoSingle:) withObject:post_id afterDelay:0.3];

编辑2.0:

现在就注意......你正在解雇你的UIViewController,而你正试图从刚被解雇的UIViewController中推出一个新的。{1}}。从理论上讲,这里:

 [self dismissViewControllerAnimated:YES completion:^{
                    NSLog(@"Dismiss completed");
                    [self pushtoSingle:post_id];
                }];

在完成时,self将为零,因为您只是将其解雇。从当前UIViewController推送新UIViewController。例如,如果您当前的UIViewController是B:

A -> B

解雇后:

A

推新的:

A -> C

答案 2 :(得分:0)

这更多只是添加上面提供的机器人6提供的答案。

如果您要将新视图添加为子视图,您还应该考虑将以下内容添加到父视图中。

-(void)viewDidDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

} 

在我的情况下,我正在启动基于通过通知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];
        }
}

发生了什么事。

Alert from parent view in child view

所以,通过上面的回答,我了解到你在视图1中所做的任何事情都会在你的子视图中再次出现,好像它仍然在你的父视图中,因为你从未删除过NSNotification观察者并且观察者仍处于活动状态。

现在这可能不会给每个人带来问题,但是如果你真的不需要它会占用系统资源,因为ARC不会释放观察者,直到父视图不再活动或除非你告诉它在你的{{{ 1}}

答案 3 :(得分:0)

类别:

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)
    }
}