2012-07-09 28 views
13

Tôi chỉ tự hỏi liệu nó có thể đẩy đến một ViewController ngay sau khi tôi loại bỏ nó không.dismissViewControllerAnimated: completion: Đẩy sang ViewController khác

Tôi đã cố gắng với điều này:

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

Và đây:

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

Nhưng không thành công. Chế độ xem được loại bỏ thành công nhưng việc đẩy không bao giờ được khởi tạo. Có cách nào khác được biết về vấn đề này không?

Trả lời

28

Yep, tôi figured it out. Tôi chỉ đơn giản là gửi một thông báo sau khi tôi bác bỏ ViewController B đến A và sau đó nhận được thông báo trong A và đẩy lên C ..

sa thải trong B:

[self dismissViewControllerAnimated:YES completion:^{ 

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

Tiếp nhận trong 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]; 

} 

Cảm ơn, Jacky Boy!

+0

câu trả lời tuyệt vời ... chỉ muốn thêm rằng đó là một ý tưởng hay để làm –

1

Đưa ra hoạt ảnh. Như thế này:

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

Tôi có trường hợp nhiều hoạt ảnh sẽ tạo ra hành vi lạ.

Sửa 1.0:

Hãy thử điều này:

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

Sửa 2.0:

Chỉ cần thông báo bây giờ ... Bạn đang sa thải UIViewController của bạn và bạn đang cố gắng đẩy một cái mới từ số bạn đã bỏ qua UIViewController. Theo lý thuyết, tại đây:

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

Trên khối hoàn thành, self sẽ không phải vì bạn chỉ cần loại bỏ. Đẩy UIViewController mới từ UIViewController hiện tại. Ví dụ, nếu hiện tại UIViewController của bạn là B:

A -> B 

Sau khi sa thải:

A 

Đẩy mới:

A -> C 
+0

Nope, không làm điều đó. Cố gắng để biến các hình ảnh động đẩy ra là tốt, nhưng điều đó đã không làm điều đó. – 278204

+0

Kiểm tra Chỉnh sửa của tôi 1.0. – Peres

+0

Không, tôi nhận được một tin nhắn tường trình từ pushtoSingle nhưng nó sẽ không đẩy. Tôi đã thử performselector cả bên ngoài và bên trong của khối hoàn thành .. – 278204

0

Đây chỉ là thêm vào câu trả lời cung cấp ở trên được trình bày bởi robot6.

Nếu bạn sắp thêm chế độ xem mới dưới dạng chế độ xem con, bạn cũng nên cân nhắc thêm các mục sau vào chế độ xem gốc.

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

} 

Trong trường hợp của tôi, tôi đã tung ra một AlertView dựa tắt của int trôi qua trở lại thông qua các thông báo NSDictionary.Then tôi đã trình bày quan điểm tương tự con lại sau khi chấp nhận 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 những gì đang xảy ra.

Alert from parent view in child view

Vì vậy, thông qua các câu trả lời trên, chúng tôi biết rằng về cơ bản bất cứ điều gì bạn làm theo quan điểm 1 sẽ đi một lần nữa theo quan điểm của con bạn như thể nó vẫn còn trên xem cha mẹ của bạn như bạn không bao giờ loại bỏ các quan sát viên NSNotification và người quan sát vẫn còn hoạt động.

Bây giờ điều này có thể không gây ra sự cố cho mọi người nhưng nó sẽ ăn tài nguyên hệ thống nếu bạn không thực sự cần nó vì ARC sẽ không phát hành người quan sát cho đến khi chế độ xem gốc không còn hoạt động hoặc trừ khi bạn yêu cầu nó làm như vậy trong viewDidDisapper của bạn

0

Thể loại:

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) 
    } 
} 
Các vấn đề liên quan