【问题标题】:cant configure dataSource and delegate无法配置数据源和委托
【发布时间】:2017-02-18 06:25:06
【问题描述】:

所以我将 Xcode 和 GitHub 项目代码升级到 swift 3.0(我的项目在 Obj C 中,而我从 GitHub 使用的一些 pod 是在 Swift 中)。我遇到了一堆错误,现在我被困在这些错误上。由于某种原因,我的 floatActionButton (git here) 的数据源和委托现在不起作用。我尝试在情节提要上以编程方式设置dataSourcedelegate,但没有成功。

错误:

在“LiquidFloatingActionButton *”类型的对象上找不到属性“dataSource”

在“LiquidFloatingActionButton *”类型的对象上找不到属性“delegate”

我相信如果我解决了dataSourcedelegate 的问题,那么它将解决下面的颜色错误。

截图:

.h:

#import <UIKit/UIKit.h>

#import "ProductContentViewController.h"

#import "LiquidFloatingActionButton-swift.h"

@interface SetScreenViewController : UIViewController



<UIPageViewControllerDataSource, LiquidFloatingActionButtonDelegate, LiquidFloatingActionButtonDataSource>

...

@end

.m:

        #import "LiquidFloatingActionButton-Swift.h"

LiquidFloatingActionButton *floatingActionButton;
NSMutableArray *liquidCells;
bool *closeFloatingButtons;


NSString *videoToWatchURL;


- (void)addLiquidButton{

    //Grabs the coordinates bottom right of the screen    
    float X_Co = self.view.frame.size.width - 50;
    float Y_Co = self.view.frame.size.height - 50;

    //i subtract 10 so the button will be placed a little bit out
    X_Co = X_Co - 10;
    Y_Co = Y_Co - 10;


    //I create each cell and set the image for each button
    liquidCells = [[NSMutableArray alloc] init];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];
    [liquidCells addObject:[[LiquidFloatingCell alloc]initWithIcon:[UIImage imageNamed:@".png"]]];



    //Sets the floating button at the loaction provided
    floatingActionButton = [[LiquidFloatingActionButton alloc] initWithFrame:CGRectMake(X_Co, Y_Co, 50, 50)];
    floatingActionButton.dataSource = self;//Error here
    floatingActionButton.delegate = self;//and here.

    //I set the color of the floating button
    floatingActionButton.color = [self colorWithHexString:@"01b8eb"];




    //Enables the user interaction fuction to true so itll open or close
    floatingActionButton.userInteractionEnabled = YES;

    //Adds the flaoting button to the view
    [self.view addSubview:floatingActionButton];
}

-(NSInteger)numberOfCells:(LiquidFloatingActionButton     *)liquidFloatingActionButton{
    return liquidCells.count;
}

-(LiquidFloatingCell *)cellForIndex:(NSInteger)index{
    return [liquidCells objectAtIndex:index];
}

Pod 文件:

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

target 'Whats New' do
  # Uncomment this line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for Whats New

  target 'Whats NewTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'Whats NewUITests' do
    inherit! :search_paths
    # Pods for testing
  end

pod 'CRToast', '~> 0.0.7'

pod "LiquidFloatingActionButton"

pod 'DGActivityIndicatorView'

pod 'M13ProgressSuite'

pod 'SDWebImage', '~>3.8'

pod 'FSCalendar'

use_frameworks!






post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '3.0'
    end
  end
end









end

以下更新

我最终使用了this 项目,因为原始 GitHub 没有得到修复,感谢所有帮助,如果有人解决了这个问题,请让这里的每个人都知道!

【问题讨论】:

  • 那是 Objective-C 代码,不是 Swift 3 代码。
  • @rmaddy 啊哈!该组件已升级到 Swift 3.0,而不是 OP 的代码。
  • @rmaddy 我的项目在目标 c 中,正在使用的 GitHub 是在 Swift 中。对不起,我没有提到这个。
  • 可以显示 Podfile 的内容吗?
  • 是的,刚刚更新了问题

标签: ios objective-c git delegates datasource


【解决方案1】:

我认为问题在于您的 Objective-C 代码不知道 LiquidFloatingButton Swift 代码的“open var”s dataSourcedelegate
为了让他们知道,需要一个 Obj-c 头文件,将这些 Swift 信息公开给你的 Obj-C 代码。这在 Apple 文档“Swift and Objective-C in the Same Project”中有详细描述,在您的情况下,特别是在“导入外部框架”部分。它说:
您可以使用以下语法将框架导入到不同目标中的任何 Objective-C .m 文件中:

@import FrameworkName;

【讨论】:

  • 对不起,我应该在我的代码中添加更多细节。我确实将框架代码添加到我的项目中。检查我更新的问题,看看。
【解决方案2】:

如果 swift @protocols 没有用@objc 定义,那么你不能在objective-c 中访问它们,

例如

@objc public protocol xxxxxxxxxxxDelegate {
   func functionOne()
   func functionSecond()
}

【讨论】:

  • 这是单个 GitHub 项目的问题吗?因为我在我的 Obj C 项目中使用其他 swift GitHub 项目非常好
  • 是的,可能是,您需要检查声明了特定@protocols 的项目的文件。
【解决方案3】:

您是否在项目中添加了桥接头?要在 Objective-C 项目中使用 Swift 代码,您需要一个桥接头。这是做什么,

1.新建.swift文件

2. 将出现一个弹出窗口并询问“您想配置一个Objective-C 桥接头”。选择

3.点击你的Xcode项目文件,点击Build Settings

4.找到搜索栏并搜索Defines Module

5.将值更改为Yes

6.在App delegate中,添加以下内容:#import "YourProjectName-swift.h"

当你想使用你的 Swift 文件时,你必须添加以下行:

#Import "YourProjectName-swift.h"

【讨论】:

  • 在这个答案中做了所有事情,它仍然有同样的错误
  • 另外我使用的是 GitHub 项目,而不是我自己的 swift 文件/委托。
【解决方案4】:

我还在 Swift 3 项目中使用LiquidFloatingActionButton。请注意,git 上的项目使用 SnapKit 表示 Swift 2.3,我也必须处理它,但这超出了这里的范围。我的解决方案不是使用@objc public protocol,而是更改协议声明如下:

public protocol LiquidFloatingActionButtonDataSource {
    func numberOfCells(_ liquidFloatingActionButton: LiquidFloatingActionButton) -> Int
    func cellForIndex(_ index: Int) -> LiquidFloatingCell
}

还有:

public protocol LiquidFloatingActionButtonDelegate {
    // selected method
    func liquidFloatingActionButton(_ liquidFloatingActionButton: LiquidFloatingActionButton, didSelectItemAtIndex index: Int) 
}

只有在我的 Swift 3 项目中才能成功使用委托。

【讨论】:

  • 我的项目使用的是 Objective-C,而不是 Swift 3.0。
猜你喜欢
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 2012-02-03
相关资源
最近更新 更多