I have a ViewController in objective c (PhotoEditViewController) that instantiates a ViewController in swift (ColorPickerViewController):
- (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];
}
This is what the ColorPickerViewController looks like:
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
}
}
}
The problem I am having is that the method .setButtonColor() isn't being called. I have declared this method in the PhotoEditViewController.h:
- (void)setButtonColor:(UIColor*) color;
and I also have it in the PhotoEditViewController.mm file:
- (void)setButtonColor:(UIColor*) color {
NSLog(@"color");
}
You have to cast the colorController as ColorPickerViewController. When you're trying to set the colorController.delegate = self PhotoEditViewController thinks that colorController is generic UIViewController class and it doesn't know that it has a delegate variable.
instead of
UIViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
you need to do
ColorPickerViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
This lets PhotoEditViewController know that colorController is type of ColorPickerViewController and it has all those stored variables and delegates.
Change the implementation of you colorButtonPressed:
action to this:
- (IBAction)colorButtonPress:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
// Change the class of from UIViewController to ColorPickerViewController while instantiating
ColorPickerViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];
// Set the delegate
colorController.delegate = self;
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];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.