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 = ""
}

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