好的,所以我让我的ActivityViewController在像平常一样共享时工作正常,但是每当用户在调用ActivityViewController之后点击cancel时,我就会得到一个恼人的致命错误。

fatal error: unexpectedly found nil while unwrapping an Optional value

所以看起来我没有很好地打开我的选项。分享方法如下:
@IBAction func share(sender: UIBarButtonItem) {
        var   memeedimage = generateMemedImage()


        let activityViewController = UIActivityViewController(activityItems:[memeedimage] , applicationActivities: nil)
        presentViewController(activityViewController, animated: true, completion: nil)


        activityViewController.completionWithItemsHandler = {
            (activity, success, returneditems, error) in
            println("Activity: \(activity) Success: \(success) Items: \(returneditems) Error: \(error)")
            self.save()

            activityViewController.dismissViewControllerAnimated(true, completion:{
                let memevc:MemeTableViewController  = MemeTableViewController()
                self.presentViewController(memevc, animated: true, completion: nil)
                })
        }

share方法调用save函数,该函数使用隐式展开的可选选项生成名为meme的对象,这会导致错误:
func save(){
        var meme : MemeObject!

          meme = MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)

    }

所以我决定安全地打开meme可选值,但这引发了另一个问题
 func save(){
        var meme : MemeObject?
        if let memez = meme{
          meme = MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)
        }

        else{
            println("Optionals man")
        }

    }

现在,当对象不是nil时,将打印“Optionals man”,这是不应该发生的,share方法中的completionwithitemshandler属性没有显示应该在用户共享对象之后直接发生的表视图控制器。
MemeTableViewController的代码:
import UIKit

class MemeTableViewController : UIViewController,UITableViewDelegate,UITableViewDataSource
{
    var memesz: [MemeObject]!


    @IBOutlet var tableView: UITableView!



    override func viewWillAppear(animated: Bool) {

            super.viewWillAppear(animated)
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            memesz = appDelegate.memes
            //tableView.reloadData()


    }

    override func viewDidLoad() {


        super.viewDidLoad()
        navigationController?.hidesBarsOnSwipe = true
        navigationController?.hidesBarsOnTap = true

    }

    //reserves the number of rows needed to display the image
       func tableView(tableView : UITableView, numberOfRowsInSection section : Int)->Int
       {

            return memesz.count

        }
    //Reserves the row to be dequeued for display
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:TableViewzCell = tableView.dequeueReusableCellWithIdentifier("MemesCell", forIndexPath: indexPath) as! TableViewzCell
        let memezrow =  memesz[indexPath.row]
        cell.label1.text = memezrow.textFieldtop
        cell.label2.text = memezrow.textFieldbottom
        cell.imageview.image = memezrow.memedImage

            return cell
    }

    //Method to do something when the row is selected
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let detailController = self.storyboard!.instantiateViewControllerWithIdentifier("FullScreenMeme") as! FullScreenMeme
        detailController.meme = memesz[indexPath.row]
        self.navigationController!.pushViewController(detailController, animated: true)

    }

}

有什么帮助吗?

最佳答案

在查看了代码之后,似乎还没有初始化变量“meme”。

func save(){
        var meme : MemeObject? =  MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())

        if let memez = meme{
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)
        }

        else{
            println("Optionals man")
        }

    }
}

@IBAction func share(sender: UIBarButtonItem) {
var   memeedimage = generateMemedImage()


let activityViewController = UIActivityViewController(activityItems:[memeedimage] , applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)


activityViewController.completionWithItemsHandler = {
    (activity, success, returneditems, error) in
    println("Activity: \(activity) Success: \(success) Items: \(returneditems) Error: \(error)")
    self.save()

    let memevc  = self.storyboard!.instantiateViewControllerWithIdentifier("MemeTableViewConroller") as! MemeTableViewController

    self.presentViewController(memevc, animated: true, completion: nil)
}
}

关于ios - 快速取消ActivityViewController会导致致命错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30966988/

10-11 15:37