iOS 8 Swift - 使用 Core Data 实现共享 Sheet
iOS 8 Swift - Implementing a Share Sheet with Core Data
我正在尝试在我的 iOS 应用程序中实现共享 sheet,但出现错误。
这是我的代码:
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var backpackerSpotImageView:UIImageView!
@IBOutlet var tableView:UITableView!
var backpackerSpot:BackpackerSpot?
override func viewDidLoad() {
super.viewDidLoad()
// customizing background of tableview
self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)
// remove extra separators
self.tableView.tableFooterView = UIView(frame: CGRectZero)
// change the color of the separator
self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)
// self-sizing cells
tableView.estimatedRowHeight = 36.0
tableView.rowHeight = UITableViewAutomaticDimension
// Do any additional setup after loading the view.
if let spotImage = backpackerSpot?.spotImage
{
self.backpackerSpotImageView.image = UIImage(data:spotImage)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell
// make cell transparent so background color can be seen
cell.backgroundColor = UIColor.clearColor()
cell.mapButton.hidden = true
switch indexPath.row {
case 0:
cell.fieldLabel.text = "Name"
cell.valueLabel.text = backpackerSpot?.spotName
case 1:
cell.fieldLabel.text = "Location"
cell.valueLabel.text = backpackerSpot?.spotLocation
cell.mapButton.hidden = false
case 2:
cell.fieldLabel.text = "Notes"
cell.valueLabel.text = backpackerSpot?.spotNote
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
return cell
}
@IBAction func shareSheet(sender:AnyObject) {
let firstActivityItem = backpackerSpot?.spotName
let secondActivityItem = backpackerSpot?.spotLocation
let activityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToFlickr,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList
]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
在我声明 activityViewController
的行中,出现以下错误:
Cannot invoke 'init' with an argument list of type '(activityItems: $T2, applicationActivities: NilLiteralConvertible)'
我是开发 iOS 应用程序的新手,我遇到的最大问题之一是破译错误消息。如果有人能指出我正确的方向,我将不胜感激。谢谢。
尝试更改这些行:
let firstActivityItem = backpacker!.spotName
let secondActivityItem = backpacker!.spotLocation
两者不能为nil
好吧,我知道出了什么问题。
首先,我没有正确定义 activityViewController
。其次,我不得不打开 backpackerSpot
。我还将功能上的发件人更改为 UIBarButtonItem
.
代码如下:
@IBAction func shareSheet(sender:UIBarButtonItem) {
let firstActivityItem = backpackerSpot!.spotName
let secondActivityItem = backpackerSpot!.spotLocation
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToFlickr,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList
]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
我正在尝试在我的 iOS 应用程序中实现共享 sheet,但出现错误。
这是我的代码:
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var backpackerSpotImageView:UIImageView!
@IBOutlet var tableView:UITableView!
var backpackerSpot:BackpackerSpot?
override func viewDidLoad() {
super.viewDidLoad()
// customizing background of tableview
self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)
// remove extra separators
self.tableView.tableFooterView = UIView(frame: CGRectZero)
// change the color of the separator
self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)
// self-sizing cells
tableView.estimatedRowHeight = 36.0
tableView.rowHeight = UITableViewAutomaticDimension
// Do any additional setup after loading the view.
if let spotImage = backpackerSpot?.spotImage
{
self.backpackerSpotImageView.image = UIImage(data:spotImage)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell
// make cell transparent so background color can be seen
cell.backgroundColor = UIColor.clearColor()
cell.mapButton.hidden = true
switch indexPath.row {
case 0:
cell.fieldLabel.text = "Name"
cell.valueLabel.text = backpackerSpot?.spotName
case 1:
cell.fieldLabel.text = "Location"
cell.valueLabel.text = backpackerSpot?.spotLocation
cell.mapButton.hidden = false
case 2:
cell.fieldLabel.text = "Notes"
cell.valueLabel.text = backpackerSpot?.spotNote
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
return cell
}
@IBAction func shareSheet(sender:AnyObject) {
let firstActivityItem = backpackerSpot?.spotName
let secondActivityItem = backpackerSpot?.spotLocation
let activityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToFlickr,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList
]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
在我声明 activityViewController
的行中,出现以下错误:
Cannot invoke 'init' with an argument list of type '(activityItems: $T2, applicationActivities: NilLiteralConvertible)'
我是开发 iOS 应用程序的新手,我遇到的最大问题之一是破译错误消息。如果有人能指出我正确的方向,我将不胜感激。谢谢。
尝试更改这些行:
let firstActivityItem = backpacker!.spotName
let secondActivityItem = backpacker!.spotLocation
两者不能为nil
好吧,我知道出了什么问题。
首先,我没有正确定义 activityViewController
。其次,我不得不打开 backpackerSpot
。我还将功能上的发件人更改为 UIBarButtonItem
.
代码如下:
@IBAction func shareSheet(sender:UIBarButtonItem) {
let firstActivityItem = backpackerSpot!.spotName
let secondActivityItem = backpackerSpot!.spotLocation
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToFlickr,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeAddToReadingList
]
self.presentViewController(activityViewController, animated: true, completion: nil)
}