2012-07-09 42 views
13

我只是想知道是否有可能推遲到一個ViewController權利後,我駁回了一個。dismissViewControllerAnimated:完成:推到另一個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]; 
       }]; 
} 

,但沒有成功。視圖成功解除了,但推送從未初始化.. 是否有另一種已知的解決方法?

回答

28

是的,我想它了。我只是張貼了通知我被解僱後的視圖控制器B到A,然後在收到該通知,並推到C .. B中

解聘:

[self dismissViewControllerAnimated:YES completion:^{ 

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

在接收:

-(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!

+0

偉大的答案...只是想補充說,它是一個好主意 – 2014-11-13 21:47:08

1

取出動畫。像這樣:

[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將是零,因爲你剛剛駁回。從目前的UIViewController推新的UIViewController。例如,如果你目前UIViewController是B:

A -> B 

解僱後:

A 

推新:

A -> C 
+0

不,沒有做到這一點。試圖將推動動畫關閉,但這並沒有做到這一點。 – 278204 2012-07-09 07:32:52

+0

檢查我的編輯1.0。 – Peres 2012-07-09 07:34:31

+0

不,我從pushtoSingle得到一條日誌消息,但它不會推送。我嘗試完成塊的外部和內部執行選擇器.. – 278204 2012-07-09 07:41:09

0

這更多的只是添加到上面提供的機器人6提供的答案。

如果您打算將新視圖添加爲子視圖,那麼您應該考慮在父視圖中添加以下內容。

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

} 

在我來說,我是通過NSDictionary.Then我接受AlertView後再次呈現相同的子視圖通知推出基於斷爲int的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

因此,通過上面的答案,我瞭解到,基本上任何你在視圖做會在你的子視圖又來了,好像它仍然在你的父視圖,你永遠不會被刪除的NSNotification觀察者和觀察員仍然活躍。

現在,這可能不會導致每個人的問題,但如果你不真的需要它,它會吃掉系統資源,因爲ARC不會釋放觀察者,直到父視圖不再活動或除非你告訴它在你的viewDidDisapper

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) 
    } 
} 
相關問題