我在目标c(PhotoEditViewController)中有一个ViewController,可以在swift(ColorPickerViewController)中实例化一个ViewController:
- (IBAction)colorButtonPress:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
UIViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
colorController.modalPresentationStyle = UIModalPresentationPopover;
colorController.preferredContentSize = CGSizeMake(284, 446);
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController = [colorController popoverPresentationController];
presentationController.sourceView = sender;
presentationController.sourceRect = CGRectMake(0, 0, 85, 30);
presentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
presentationController.delegate = self;
[self presentViewController:colorController animated:YES completion:nil];
}
这是ColorPickerViewController的样子:
import UIKit
class ColorPickerViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// Global variables
var tag: Int = 0
var color: UIColor = UIColor.gray
var delegate: PhotoEditViewController? = nil
// This function converts from HTML colors (hex strings of the form '#ffffff') to UIColors
func hexStringToUIColor (_ hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if (cString.characters.count != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
// UICollectionViewDataSource Protocol:
// Returns the number of rows in collection view
internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
// UICollectionViewDataSource Protocol:
// Returns the number of columns in collection view
internal func numberOfSections(in collectionView: UICollectionView) -> Int {
return 16
}
// UICollectionViewDataSource Protocol:
// Inilitializes the collection view cells
internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.clear
cell.tag = tag
tag = tag + 1
return cell
}
// Recognizes and handles when a collection view cell has been selected
internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
var colorPalette: Array<String>
// Get colorPalette array from plist file
let path = Bundle.main.path(forResource: "colorPalette", ofType: "plist")
let pListArray = NSArray(contentsOfFile: path!)
if let colorPalettePlistFile = pListArray {
colorPalette = colorPalettePlistFile as! [String]
var cell: UICollectionViewCell = collectionView.cellForItem(at: indexPath)! as UICollectionViewCell
var hexString = colorPalette[cell.tag]
color = hexStringToUIColor(hexString)
self.view.backgroundColor = color
NSLog("Send delegate message")
delegate?.setButtonColor(color) // THIS IS WHAT ISN'T BEING CALLED
}
}
}
我遇到的问题是没有调用.setButtonColor()方法。我已经在PhotoEditViewController.h中声明了此方法:
- (void)setButtonColor:(UIColor*) color;
我在PhotoEditViewController.mm文件中也有它:
- (void)setButtonColor:(UIColor*) color {
NSLog(@"color");
}
最佳答案
您必须将colorController强制转换为ColorPickerViewController。当您尝试设置colorController.delegate = self时,PhotoEditViewController认为colorController是通用的UIViewController类,并且不知道它具有委托变量。
代替
UIViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
你需要做
ColorPickerViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
这使PhotoEditViewController知道colorController是ColorPickerViewController的类型,并且具有所有存储的变量和委托。
关于ios - 从Swift ViewController向 objective-c ViewController发送消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40027125/