从swift ViewController发送消息到Objective c ViewController

时间:2016-10-13 17:12:27

标签: ios objective-c swift uiviewcontroller

我在目标c(PhotoEditViewController)中有一个ViewController,它在swift中实例化一个ViewController(ColorPickerViewController):

#menuDropdown {
  position: absolute;
  transition: (some long ass transition code);  
}

#menuDropdown.show {
    height: 460px;
}

#menuDropdown.hide {
    height: 0px;
}

#innerDropdown.show {
    display: block;
    animation: fadein .15s;
}

这就是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];

}

我遇到的问题是没有调用方法.setButtonColor()。我在PhotoEditViewController.h中声明了这个方法:

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

我也在PhotoEditViewController.mm文件中有它:

- (void)setButtonColor:(UIColor*) color;

2 个答案:

答案 0 :(得分:0)

您必须将colorController转换为ColorPickerViewController。当您尝试设置colorController.delegate = self时PhotoEditViewController认为colorController是通用的UIViewController类,并且它不知道它有一个委托变量。

而不是

UIViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];

你需要做

ColorPickerViewController *colorController = [storyboard instantiateViewControllerWithIdentifier:@"colorPickerPopover"];

这让PhotoEditViewController知道colorController是ColorPickerViewController的类型,它拥有所有存储的变量和委托。

答案 1 :(得分:0)

将您colorButtonPressed:操作的实施更改为:

- (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];
}