gpt4 book ai didi

ios - Swift 取消 ActivityViewController 导致 fatal error

转载 作者:行者123 更新时间:2023-11-28 09:05:23 28 4
gpt4 key购买 nike

好吧,我让我的 ActivityViewController 在正常共享时正常工作,但是每当用户在调用 ActivityViewController 后点击取消时,我都会收到恼人的 fatal error 。

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 属性没有呈现 TableView Controller ,这应该在用户共享对象后直接发生。

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)
}
}

rembember to set storyboardID

关于ios - Swift 取消 ActivityViewController 导致 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com