我正在尝试将网上商店添加到我的应用中。问题是网上商店为我提供了一个Obj-C框架,我只熟悉在Swift中制作iOS应用程序。
我设法设置了一个Obj-C桥接头并实例化了webshop对象。
我的项目如下:
当我的CodeDetailViewController上的商店按钮被点击时,此功能被触发:
@IBAction func shopButtonPressed(sender: UIButton){
let instanceOfShop : Shop = Shop()
instanceOfShop.showShop()
}
我的Shop.m看起来像这样:
#import <Foundation/Foundation.h>
#import <FMShop/FMShop.h>
#import "Shop.h"
@implementation Shop
- (void) showShop {
//SANDBOX
FMShop* shop = [[FMShop alloc] initWithKey:@"4M7HDPAQMY68S"
storeKey:@"C8RSHBH710GK8"
buyerID:nil
sandbox:YES
controller:nil
delegate:nil];
[shop showStore:kShopViewHome ID:nil];
}
@end
此类中的showShop函数被触发。然而,没有任何东西出现。
关于如何实施此网店的手册针对的是Obj-C应用程序。关于代表,它说:
委托:请定义实现委托的类或接口。没有这样,商店将不会显示。请访问FMShopDelegate以查看支持的方法和回调列表。
这是我在框架中可以找到的:
@protocol FMShopDelegate <NSObject>
@optional
-(void)shopWillAppear:(FMShop*)shop;
-(void)shopDidAppear:(FMShop*)shop;
-(void)shopWillDisappear:(FMShop*)shop;
-(void)shopDidDisappear:(FMShop*)shop;
-(void)shopProductInfo:(FMShop*)shop products:(NSArray*)productArray;
-(void)shopHistoryOrderInfo:(FMShop*)shop orders:(NSArray*)orderArray;
-(void)shopPlaceOrderSuccess:(FMShop*)shop orderID:(NSString*)orderID;
-(void)shopOrderUserCancelled:(FMShop*)shop orderID:(NSString*)orderID;
-(void)shopFailed:(FMShop*)shop error:(FMShopErrorType)error;
@end
有谁能告诉我如何将这个最终方法连接到正确的代表?
答案 0 :(得分:1)
听起来如果在实例化FMShop实例时没有提供委托,那么show方法将无效。在Shop.m文件中,您使用nil
作为委托参数来实例化FMShop对象。
尝试使对象符合FMShopDelegate
协议,并将此对象作为委托参数传递。您甚至可以让Shop对象符合协议本身,并将self
作为委托,如果这样可以满足您的需要。
你可以在一个新的Shop.swift文件中或在Objective-C的Shop.m文件中在Swift中执行此操作:
夫特:
class Shop: NSObject {
func showShop() {
// I'm guessing on the syntax here, Xcode will help autocomplete the correct functions
let shop = FMShop(key: KEY, storeKey: STOREKEY...)
shop.show(store: kShopViewHome, id: nil)
}
}
extension Shop: FMShopDelegate {
// Implement any of the protocol functions you want here
}
目标-C:
@interface Shop() <FMShopDelegate> // Conform to the delegate here
@end
@implementation Shop
- (void) showShop {
//SANDBOX
FMShop* shop = [[FMShop alloc] initWithKey:@"4M7HDPAQMY68S"
storeKey:@"C8RSHBH710GK8"
buyerID:nil
sandbox:YES
controller:nil
delegate:self];
[shop showStore:kShopViewHome ID:nil];
}
// Implement any of the protocol methods here
@end
答案 1 :(得分:0)
答案 2 :(得分:0)