2012-03-07 34 views
9
đầu tiên

sử dụng đoạn mã sau trong các phụ huynh ViewController, tôi muốn trình bày một cái nhìn thứ hai ontop những người đầu tiên, sau đó sa thải đầu tiên:Làm thế nào để trình bày một ViewController thứ hai và loại bỏ các

// Animates the next questionViewController using the first questionViewController 
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil]; 

// Dismiss the first questionViewController 
[previousView dismissViewControllerAnimated:NO completion:nil]; 

Khi chạy, các lần xem thứ hai được trình bày, nhưng chế độ xem đầu tiên sẽ không loại bỏ.

+0

previousView là một viewController trên một bộ điều khiển chuyển hướng ngăn xếp hoặc nó được hiển thị modally? –

+0

Có, nó được hiển thị một cách bình thường. Tôi không thể hiểu tại sao nó sẽ không bỏ qua. –

+0

Từ mã của bạn, nó chỉ nói presentViewController: animated, not presentModalViewController: animated –

Trả lời

12

Bạn sẽ cần phải đầu tiên bỏ qua những "previousView" & sau đó trình bày các "nextQuestionViewController":

// Dismiss the first questionViewController 
[previousView dismissViewControllerAnimated:NO completion:nil]; 

// Animates the next questionViewController using the first questionViewController 
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil]; 
+1

Theo sau kết quả trả lời trong trường hợp ngoại lệ sau: 'Chấm dứt ứng dụng do không ngoại lệ 'NSInvalidArgumentException', lý do: 'Ứng dụng đã cố gắng trình bày một bộ điều khiển hoạt động theo phương thức .' ' – Benjamin

+0

Bạn có nghĩ rằng đó sẽ là lỗi dễ sử dụng một bộ điều khiển xem để trình bày một bộ điều khiển khác biết rằng bản thân nó sẽ được xử lý tại một thời điểm nào đó. Tôi thà sử dụng '[[UIApplication sharedApplication] .keyWindow.rootViewController' hoặc tương tự với' nextQuestionViewController' hiện tại, như được đề xuất bởi @Mozilla. – atulkhatri

4

thử

[self dismissViewControllerAnimated:NO completion:nil]; 

thất bại rằng:

[self.navigationController popViewControllerAnimated:YES]; 
+0

Tôi đang cố gắng giải quyết vấn đề này một lần nữa - sử dụng cả hai phương pháp đó đều không thể loại bỏ khung nhìn thứ haiController, sau khi trình bày đầu tiên - tôi có thiếu cái gì khác không? –

+0

Như Raphael nói, bạn nên trình bày viewController thứ hai như sau: [previousView presentModalViewController: nextQuestionViewController animated: YES]; – ader

2

tôi đã làm tiếp theo (tự - là bộ điều khiển cũ của bạn):

UIStoryboard *storyboard = self.storyboard; 

[self dismissViewControllerAnimated:YES 
         completion:^{ 
     UIViewController *newController = [storyboard instantiateViewControllerWithIdentifier:@"newControllerStoryboardId"]; 
     newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 

     [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:newController animated:YES completion:nil]; 
    }]; 
-1

Dường như không thể đi từ B đến C mà không hiển thị một thời gian ngắn, trông không chuyên nghiệp. Tuy nhiên, bạn có thể đặt một subview đen trên đầu trang của A cho đến khi bạn đã hoạt hình để C.

Đối với mã, xem câu trả lời của tôi tại https://stackoverflow.com/a/45579371/218226

Các vấn đề liên quan