2015-01-28 34 views
6

Chúc các bạn ngày tốt lành, tôi đang học Swift, cần một số trợ giúp ở đây.Swift popToViewController

Người dùng đang đăng ký và chọn hình ảnh của họ. Sau khi loại bỏ bộ chọn hình ảnh, tôi muốn có ComposeViewController xuất hiện.

Đây là mã:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) { 
    let pickedImage:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage 

    //Scale Down Image 
    let scaledImage = self.scaleImageWith(pickedImage, and: CGSizeMake(100,100)) 

    let imageData = UIImagePNGRepresentation(scaledImage) 

    let imageFile:PFFile = PFFile(data: imageData) 

    PFUser.currentUser().setObject(imageFile, forKey: "profileImage") 
    PFUser.currentUser().saveInBackgroundWithTarget(nil, selector: nil) 

    picker.dismissViewControllerAnimated(true, completion: nil) 

    //this is the line seems to have problem. 
    self.navigationController?.popToViewController(ComposeViewController, animated: true) 
} 

Sau đó, tôi nhận được những lỗi: ComposeViewController.Type 'không phải là chuyển đổi thành' UIViewController Dự kiến ​​tên thành viên hoặc gọi constructor sau tên loại

Nó có gợi ý để khắc phục bằng cách đặt() sau ComposeViewController nhưng sau đó nó đưa ra nhiều lỗi hơn sau khi sửa chữa.

Hy vọng ai đó có thể trợ giúp. Cảm ơn! :-)

+1

Chào mừng bạn đến SO !: Nhưng những gì chính xác là câu hỏi/vấn đề của bạn? Bạn có nhận được một thông báo lỗi? ComposeViewController của bạn có xuất hiện không? (vui lòng chỉnh sửa câu hỏi của bạn và thêm thông tin này) –

+0

Cảm ơn Frank! Tôi sẽ lưu ý điều đó vào lần sau. :-) – bobster

Trả lời

0

trình điều khiển điều hướng duy trì chồng lượt xem bạn đang đẩy. Nó giống như một hàng cuối trong hàng đầu tiên.

Để bật lên ComposeViewController, chế độ xem đó phải tồn tại trong hàng đợi và bạn nên tham chiếu đến nó.

Bạn sẽ cần phải chuyển thể hiện của ComposeViewController. để đơn giản bạn có thể lưu tham chiếu đó trong appdelegate. (phương pháp này không được khuyến nghị)

2

Tôi đã kết thúc thay thế mã sau bên trong chế độ xem chính và nó hoạt động. Tôi không chắc chắn nếu điều này là đúng cách, bạn sẽ nhớ cho tôi một số ý kiến?

//self.navigationController?.popToViewController(ComposeViewController, animated: true)  
let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as ComposeViewController 

    self.navigationController?.pushViewController(switchViewController, animated: true) 

Tôi đã xác định "view2" làm ID bảng phân cảnh đích.

4

Có một phương pháp cho phép bạn truy cập vào một mảng của tất cả các ViewControllers trên stack hiện tại, và bạn có thể chụp một trong những bạn muốn bằng cách sử dụng chỉ mục của nó, ví dụ:

let switchViewController = self.navigationController?.viewControllers[1] as! ComposeViewController 

self.navigationController?.popToViewController(switchViewController, animated: true) 
1
if let composeViewController = self.navigationController?.viewControllers[1] { 
      self.navigationController?.popToViewController(composeViewController, animated: true) 
} 
0
for (var i = 0; i < self.navigationController?.viewControllers.count; i++) 
{ 
    if(self.navigationController?.viewControllers[i].isKindOfClass(DestinationViewController) == true) 
    { 
     self.navigationController?.popToViewController(self.navigationController!.viewControllers[i] as! DestinationViewController, animated: true) 

    break; 
    } 
} 
4

Tôi biết điều này là cũ, nhưng nó giống như những gì Saqib nói, bạn không thể bật đến một bộ điều khiển xem mà không tồn tại được nêu ra.

Rất nhiều câu trả lời ở đây dường như là từ những người không đọc câu hỏi của bạn, chỉ là tiêu đề. Tôi sẽ để lại mã này ở đây trong trường hợp nó giúp bất cứ ai.

let vcIndex = self.navigationController?.viewControllers.indexOf({ (viewController) -> Bool in 

    if let _ = viewController as? ComposeViewController { 
     return true 
    } 
    return false 
}) 

let composeVC = self.navigationController?.viewControllers[vcIndex!] as! ComposeViewController 

self.navigationController?.popToViewController(composeVC, animated: true) 
-1
let controllers = self.navigationController?.viewControllers 
       for vc in controllers! { 
       if vc is YourVC { 
        _ = self.navigationController?.popToViewController(vc as! YourVC, animated: true) 
       } 
      }