How set title of a WKInterfaceButton? - objective-c

I'm trying to change the text of a button once it is pressed however it doesn't work. Am i missing something?
i've been trying to figure my problem for hours.
any help would be appreciated.
h. file
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#interface InterfaceController : WKInterfaceController
{
IBOutlet WKInterfaceButton*playpausebtn;
}
-(IBAction)play;
#end
m. file
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
#interface InterfaceController() <WCSessionDelegate>
#end
#implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
}
- (void)willActivate {
[super willActivate];
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
-(IBAction)play{
[playpausebtn setTitle:#"sleep"];
}

It is working fine in Swift-3 Xcode-8.1 .
// InterfaceController.swift
// WatchKit Extension
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController,WCSessionDelegate {
#IBOutlet var textLabel: WKInterfaceLabel!
var session:WCSession?
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
checkSupportOfSession()
changeAttributeOfText()
}
#IBOutlet var buttonOutlet: WKInterfaceButton!
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func checkSupportOfSession() {
if( WCSession.isSupported() ) {
self.session = WCSession.default()
self.session?.delegate = self
self.session?.activate()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("session")
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
let message:String = message["textIndex"] as! String
textLabel.setText(message)
print(message)
}
func changeAttributeOfText() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let font = UIFont.boldSystemFont(ofSize: 12)
let attributes:Dictionary = [NSParagraphStyleAttributeName:paragraphStyle , NSFontAttributeName:font ]
let attributeString:NSAttributedString = NSAttributedString(string: "HELLO", attributes: attributes)
textLabel.setAttributedText(attributeString)
}
//Change the ButtonTitle after click
#IBAction func buttonClicked() {
buttonOutlet.setTitle("textNew")
}}
Demo App

Related

How to add swizzling method to UIApplication using Swift?

I have a code below trying to use swizzling method to UIApplication methods. But the methods not getting called. Meanwhile, I'm trying to add this code into a framework or a private pod.
extension UIApplication {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
struct SwizzlingSelector {
let original:Selector
let swizzled:Selector
}
// make sure this isn't a subclass
if self !== UIApplication.self {
return
}
dispatch_once(&Static.token) {
let selectors = [
SwizzlingSelector(
original: Selector("application:didFinishLaunchingWithOptions:"),
swizzled: Selector("custome_application:didFinishLaunchingWithOptions:")
),
SwizzlingSelector(
original: Selector("applicationDidEnterBackground:"),
swizzled: Selector("custom_applicationDidEnterBackground:")
)
]
for selector in selectors {
let originalMethod = class_getInstanceMethod(self, selector.original)
let swizzledMethod = class_getInstanceMethod(self, selector.swizzled)
let didAddMethod = class_addMethod(self, selector.original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, selector.swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
}
}
// MARK: - Method Swizzling
public func custome_application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
print("is here")
return true
}
func custom_applicationDidEnterBackground(application: UIApplication) {
print("is herer")
}
}
I think you should implement this swizzling method in your AppDelegate but not as extension of UIApplication. Cause you swizzle your AppDelegate's methods but not UIApplication's methods.
extension YourAppDelegate {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
struct SwizzlingSelector {
let original:Selector
let swizzled:Selector
}
// make sure this isn't a subclass
if self !== YourAppDelegate.self {
return
}
dispatch_once(&Static.token) {
let selectors = [
SwizzlingSelector(
original: Selector("application:didFinishLaunchingWithOptions:"),
swizzled: Selector("custome_application:didFinishLaunchingWithOptions:")
),
SwizzlingSelector(
original: Selector("applicationDidEnterBackground:"),
swizzled: Selector("custom_applicationDidEnterBackground:")
)
]
for selector in selectors {
let originalMethod = class_getInstanceMethod(self, selector.original)
let swizzledMethod = class_getInstanceMethod(self, selector.swizzled)
let didAddMethod = class_addMethod(self, selector.original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, selector.swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
}
}
// MARK: - Method Swizzling
public func custome_application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
print("is here")
return true
}
func custom_applicationDidEnterBackground(application: UIApplication) {
print("is herer")
}
}

Objective-C to Swift Conversion Issue

I am trying to convert Apple's own sample code which is only written in Objective-C in to Swift.
This is what's in Objective C
.h
#import UIKit;
#class AVCaptureSession;
#interface AAPLPreviewView : UIView
#property (nonatomic) AVCaptureSession *session;
#end
.m
#import AVFoundation;
#import "AAPLPreviewView.h"
#implementation AAPLPreviewView
+ (Class)layerClass
{
return [AVCaptureVideoPreviewLayer class];
}
- (AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.layer;
return previewLayer.session;
}
- (void)setSession:(AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.layer;
previewLayer.session = session;
}
#end
So far, this what I came up for Swift:
import AVFoundation
import UIKit
class CamPreviewLayer: UIView
{
var session: AVCaptureSession!
override class func layerClass() -> AnyClass
{
return AVCaptureVideoPreviewLayer
}
func session() -> AVCaptureSession {
var previewLayer: AVCaptureVideoPreviewLayer = self.layer as! AVCaptureVideoPreviewLayer
return previewLayer.session
}
func setSession(session: AVCaptureSession) {
let previewLayer: AVCaptureVideoPreviewLayer = self.layer as! AVCaptureVideoPreviewLayer
previewLayer.session = session
}
}
My Swift code has 3 errors.
You have to remove var session: AVCaptureSession like this.
import AVFoundation
import UIKit
class AVPreviewView : UIView {
override class func layerClass() -> AnyClass {
return AVCaptureVideoPreviewLayer.self
}
func session () -> AVCaptureSession {
return (self.layer as! AVCaptureVideoPreviewLayer).session
}
func setSession(session : AVCaptureSession) -> Void {
(self.layer as! AVCaptureVideoPreviewLayer).session = session;
(self.layer as! AVCaptureVideoPreviewLayer).videoGravity = AVLayerVideoGravityResizeAspect;
}
}
Hope it help!

How to invoke [self new] in Swift

Here is an UIView extension written in ObjectiveC to easily create view for using Auto-layout:
+(id)autolayoutView
{
UIView *view = [self new];
view.translatesAutoresizingMaskIntoConstraints = NO;
return view;
}
it invokes [self new] so any subclass of UIView can use this method. How can I achieve this in Swift?
OK, this appears to be the solution. The type must have a required initializer with the correct parameter list (in this case no parameters).
class SubView: UIView {
override required init() {
super.init()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
class func autolayoutView() -> UIView {
var view = self()
view.setTranslatesAutoresizingMaskIntoConstraints(false)
return view
}
}
Though both Carrl and Gregory Higley are right with there solutions, including thr remark about the fact that self() needs to use a required init i wanted to post a more general example:
class Human {
var gender = String()
required init() {
self.gender = "any"
}
class func newHuman() -> Human {
return self()
}
}
class Man : Human {
required init() {
super.init()
self.gender = "male"
}
}
var someMan = Man.newHuman()
println(someMan.gender) // male
Inspired by Gregory Higley, i think the solution is here:
extension UIView{
class func autolayoutView() -> UIView {
var view = self()
view.setTranslatesAutoresizingMaskIntoConstraints(false)
return view
}
}
Update for Swift2.1:
extension UIView{
class func autolayoutView() -> UIView {
let view = self.init()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
}

Unwrap UIWindow twice from AppDelegate in Swift

To Get the rootViewController in Objective-c you can use the below line of code
[[[UIApplication sharedApplication] delegate] window].rootViewController
I tried to do the same in swift
UIApplication.sharedApplication().delegate?.window?.rootViewController
But I got this error
'UIWindow?' does not have a member named 'rootViewController'
And the suggestion says that you have to unwrap UIWindow twice UIWindow?? to be
UIApplication.sharedApplication().delegate?.window??.rootViewController
My Question is: Why do I need to unwrap the window twice ?
.
.
I have looked in the API and found that
protocol UIApplicationDelegate : NSObjectProtocol {
optional var window: UIWindow? { get set }
}
The window has one optional
class UIWindow : UIView {
var rootViewController: UIViewController?
}
Also the rootViewController has one optional
.
.
I thought may be because UIWindow in UIApplicationDelegate protocol has optional and UIWindow? so I tried the below in Playground
#objc protocol MyApplicationDelegate {
optional var myWindow: MyWindow? { get set }
}
class MyWindow : NSObject {
var rootViewController: Int?
init(number: Int) {
rootViewController = number
}
}
class MyAppDelegate: MyApplicationDelegate {
var myWindow: MyWindow?
init() {
myWindow = MyWindow(number: 5)
}
}
let myDelegate = MyAppDelegate()
println(myDelegate.myWindow?.rootViewController)
However I can get myWindow with one optional and can successfully log '5'
What am I missing here?
Well; I found the problem with my example
In my example I'm creating object from MyAppDelegate directly which will sure have myWindow property as I'm defining it
Changing the example to be the following
#objc protocol MyApplicationDelegate {
optional var myWindow: MyWindow? { get set }
}
class MyWindow : NSObject {
var rootViewController: Int?
init(number: Int) {
rootViewController = number
}
}
class MyAppDelegate: MyApplicationDelegate {
var myWindow: MyWindow?
init() {
myWindow = MyWindow(number: 5)
}
}
class MyApplication: NSObject {
var myDelegate: MyApplicationDelegate
override init() {
myDelegate = MyAppDelegate()
}
}
let myApplication = MyApplication()
println(myApplication.myDelegate.myWindow??.rootViewController)
I needed to add another class MyApplication that has a property conform toMyApplicationDelegate
So I was able to use myWindow?? first unwrapping for the optional part in the protocol and second one is for the optional declaration in the variable UIWindow?

Swift accessing class variable inside closures

Why in Objective-C I can set instance variable inside a block:
#interface CMVServices : UIViewController
#property (nonatomic, strong) NSMutableArray *times;
#implementation CMVServices
#synthesize times=_times;
and set the _times instance variable inside a block:
(some code )
.
.
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[_times addObjectsFromArray:objects];
}
but I can't in Swift?
class ViewController: UIViewController
var times :AnyObject[]!
query.findObjectsInBackgroundWithBlock { (objects: AnyObject[]!, error: NSError!) -> Void in
self.times = objects
}
The message is:
(AnyObject[]?) times = parent failed to evaluate: variable not available
Two things:
Swift syntax for Array declarations has changed from AnyObject[] to [AnyObject]
If you're still in the class scope, simply use times instead of self.times.
This works for me in XCode6-Beta4 in a Playground file:
import UIKit
class CMVServices : UIViewController {
var times : [AnyObject] = []
func testFunc() {
func findObjects(objects : [AnyObject]!, error : NSError!) {
times = objects
var test_str = "still evaluating in Playground here"
}
findObjects(["test", "string"], nil)
}
}
var test = CMVServices()
test.testFunc()
and for full-on test of closures themselves, this also works (again, in XCode6-Beta4):
import UIKit
class CMVServices : UIViewController {
var times: [AnyObject] = []
func testClosure() {
{ (objects : [AnyObject]!, error : NSError!) -> Void in
self.times = objects
NSLog("still evaluating")
}(["new", "test", "string"], nil)
}
}
var test = CMVServices()
test.testClosure()
test.times // ["new", "test", "string"]