16

我只是想知道是否可以在我解雇一个 ViewController 后立即推送到一个 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

4 回答 4

31

是的,我想通了。在我将 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];

}

谢谢,杰基男孩!

于 2012-07-09T08:02:15.233 回答
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。理论上,这里:

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

在您的完成块上,self将为 nil,因为您只是将其关闭。UIViewController从当前推送新的UIViewController。例如,如果您的电流UIViewController是 B:

A -> B

解散后:

A

推新的:

A -> C
于 2012-07-09T07:24:23.490 回答
0

这更多地只是增加了上面由robot6提供的答案。

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

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

这是发生了什么。

子视图中来自父视图的警报

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

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

于 2014-11-13T22:03:04.073 回答
0

类别:

class Category {
    var name: String = ""
}

第一视图控制器:

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

}

第二视图控制器:

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

}

第三视图控制器:

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)
    }
}
于 2014-11-22T07:51:34.830 回答