From 5b6e4955ffb781b560fc54bea828a35c549b9f69 Mon Sep 17 00:00:00 2001 From: Peter Prokop Date: Thu, 26 Nov 2015 12:12:19 +0300 Subject: [PATCH] RatingView is now usable from ObjC, fixed error with initWithFrame method delegate property is weak now --- StarryStars/RatingView.swift | 6 ++- StarryStarsExample.xcodeproj/project.pbxproj | 14 +++++++ StarryStarsExample/Base.lproj/Main.storyboard | 20 ++++++++++ StarryStarsExample/ObjCViewController.h | 13 +++++++ StarryStarsExample/ObjCViewController.m | 37 +++++++++++++++++++ .../StarryStarsExample-Bridging-Header.h | 4 ++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 StarryStarsExample/ObjCViewController.h create mode 100644 StarryStarsExample/ObjCViewController.m create mode 100644 StarryStarsExample/StarryStarsExample-Bridging-Header.h diff --git a/StarryStars/RatingView.swift b/StarryStars/RatingView.swift index 75adf08..3c10881 100644 --- a/StarryStars/RatingView.swift +++ b/StarryStars/RatingView.swift @@ -7,7 +7,7 @@ import UIKit -public protocol RatingViewDelegate { +@objc public protocol RatingViewDelegate { /** Called when user's touch ends @@ -53,13 +53,15 @@ public class RatingView: UIView { /// Delegate, must confrom to *RatingViewDelegate* protocol - @IBInspectable public var delegate: RatingViewDelegate? + public weak var delegate: RatingViewDelegate? var stars = [UIImageView]() override init(frame: CGRect) { super.init(frame: frame) + + customInit() } required public init?(coder aDecoder: NSCoder) { diff --git a/StarryStarsExample.xcodeproj/project.pbxproj b/StarryStarsExample.xcodeproj/project.pbxproj index 8503167..42f2368 100644 --- a/StarryStarsExample.xcodeproj/project.pbxproj +++ b/StarryStarsExample.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 0D04DE721C07009700EBDC42 /* ObjCViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D04DE711C07009700EBDC42 /* ObjCViewController.m */; }; 0D2DE7131BE5F23E005DBFEC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2DE7091BE5F23E005DBFEC /* AppDelegate.swift */; }; 0D2DE7151BE5F23E005DBFEC /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0D2DE70B1BE5F23E005DBFEC /* LaunchScreen.storyboard */; }; 0D2DE7161BE5F23E005DBFEC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0D2DE70D1BE5F23E005DBFEC /* Main.storyboard */; }; @@ -18,6 +19,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 0D04DE6C1C07006F00EBDC42 /* StarryStarsExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "StarryStarsExample-Bridging-Header.h"; sourceTree = ""; }; + 0D04DE701C07009700EBDC42 /* ObjCViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjCViewController.h; sourceTree = ""; }; + 0D04DE711C07009700EBDC42 /* ObjCViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjCViewController.m; sourceTree = ""; }; 0D2DE7091BE5F23E005DBFEC /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 0D2DE70C1BE5F23E005DBFEC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 0D2DE70E1BE5F23E005DBFEC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -49,6 +53,9 @@ 0D2DE70D1BE5F23E005DBFEC /* Main.storyboard */, 0D2DE70F1BE5F23E005DBFEC /* Info.plist */, 0D2DE7121BE5F23E005DBFEC /* ViewController.swift */, + 0D04DE6C1C07006F00EBDC42 /* StarryStarsExample-Bridging-Header.h */, + 0D04DE701C07009700EBDC42 /* ObjCViewController.h */, + 0D04DE711C07009700EBDC42 /* ObjCViewController.m */, ); path = StarryStarsExample; sourceTree = ""; @@ -115,6 +122,7 @@ 0DEBD3931BDA134D00FC7856 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0710; LastUpgradeCheck = 0700; ORGANIZATIONNAME = "Peter Prokop"; TargetAttributes = { @@ -161,6 +169,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 0D04DE721C07009700EBDC42 /* ObjCViewController.m in Sources */, 0D2DE7201BE5F83C005DBFEC /* RatingView.swift in Sources */, 0D2DE7191BE5F23E005DBFEC /* ViewController.swift in Sources */, 0D2DE7131BE5F23E005DBFEC /* AppDelegate.swift in Sources */, @@ -274,10 +283,13 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = StarryStarsExample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Prokop.StarryStarsExample; PRODUCT_NAME = StarryStarsExample; + SWIFT_OBJC_BRIDGING_HEADER = "StarryStarsExample/StarryStarsExample-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; }; name = Debug; }; @@ -285,10 +297,12 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = StarryStarsExample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = Prokop.StarryStarsExample; PRODUCT_NAME = StarryStarsExample; + SWIFT_OBJC_BRIDGING_HEADER = "StarryStarsExample/StarryStarsExample-Bridging-Header.h"; }; name = Release; }; diff --git a/StarryStarsExample/Base.lproj/Main.storyboard b/StarryStarsExample/Base.lproj/Main.storyboard index 1957968..878e1a4 100644 --- a/StarryStarsExample/Base.lproj/Main.storyboard +++ b/StarryStarsExample/Base.lproj/Main.storyboard @@ -65,5 +65,25 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/StarryStarsExample/ObjCViewController.h b/StarryStarsExample/ObjCViewController.h new file mode 100644 index 0000000..e06f8ef --- /dev/null +++ b/StarryStarsExample/ObjCViewController.h @@ -0,0 +1,13 @@ +// +// ObjCViewController.h +// StarryStarsExample +// +// Created by Peter Prokop on 26/11/15. +// Copyright © 2015 Peter Prokop. All rights reserved. +// + +#import + +@interface ObjCViewController : UIViewController + +@end diff --git a/StarryStarsExample/ObjCViewController.m b/StarryStarsExample/ObjCViewController.m new file mode 100644 index 0000000..ea73262 --- /dev/null +++ b/StarryStarsExample/ObjCViewController.m @@ -0,0 +1,37 @@ +// +// ObjCViewController.m +// StarryStarsExample +// +// Created by Peter Prokop on 26/11/15. +// Copyright © 2015 Peter Prokop. All rights reserved. +// + +#import "ObjCViewController.h" +#import "StarryStarsExample-Swift.h" + +@interface ObjCViewController() + +@end + +@implementation ObjCViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + + RatingView* rv = [[RatingView alloc] initWithFrame:self.view.bounds]; + [self.view addSubview:rv]; + rv.editable = YES; + rv.delegate = self; +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; +} + +- (void)ratingView:(RatingView * __nonnull)ratingView didChangeRating:(float)newRating { + NSLog(@"newRating: %f", newRating); +} + + +@end diff --git a/StarryStarsExample/StarryStarsExample-Bridging-Header.h b/StarryStarsExample/StarryStarsExample-Bridging-Header.h new file mode 100644 index 0000000..1b2cb5d --- /dev/null +++ b/StarryStarsExample/StarryStarsExample-Bridging-Header.h @@ -0,0 +1,4 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// +