How to Get Title from Wkinterfacebutton

How to get title from WKInterfaceButton

You can't get the state of any WKInterface UI element like buttons (WKInterfaceButton) label (WKInterfaceLabel) etc. I think this is designed by Apple like this because it involve under the hood communication between the extension running on your iOS device & the watch App. To save this overhead there are no getters and the extension should keep track of the state change (it is the one that can change the state).

set WKInterfaceButton title color - WatchKit

Thanks to ColdLogic for pointing me in the right direction. Its a bit complicated way of changing title color but it works.

NSString *titleStr = @"Next";
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:titleStr];
[attString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, attString.string.length)];
//set color
[customBtn setAttributedTitle:attString];

Can't set WKInterfaceButton title in table

Found the problem, posting it just in case someone has the same issue. There was a lost/disconnected outlet of another button that I was not using anymore. Once I solved that, it works.

how to center title text for WKInterfaceButton with iOS WatchKit

The WKInterfaceButton class has no titleLabel property, which is the reason you are getting this error. To set the alignment of the title, use its attributedTitle property.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourTitle attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
[myWKButton setAttributedTitle:attributedString];

WKInterfaceButton multiple line title

So you want multiline actionable text in your storyboard.

For now we can not make the multiline title of the WKInterfaceButton. But there is another work around that is helpful and recommended by Apple too.

Change the Content type of the WKInterfaceButton from Text to Group.

Make sure its height is "Size To Fit Content".

Now Add a WKInterfaceLabel in this Group. Make this Label's proper IBOutlet Connection. Put your Title actionable Text in this Label. Make sure Label's height is "Size To Fit Content" too.

How to get touch events for WKInterfaceButton

It sounds silly but you could use a WKLongPressGestureRecognizer to do so.
In Interface builder add a WKLongPressGestureRecognizer e.g. to a WKInterfaceImage. Set the Min Duration from the WKLongPressGestureRecognizer to 0 as in the screenshot below.

Sample Image

Now connect the action to your action in the InterfaceController. I am using a Timer to trigger the increment but you can do this with perform(_ aSelector: Selector, with anArgument: Any?, afterDelay delay: TimeInterval) and a flag (button pressed) as well.

Consider that movements will be detected as well but this will not affect the functionality as required.

var gestureTimer:Timer?

@IBAction func gesture(_ sender: WKLongPressGestureRecognizer) {

switch sender.state {
case .began:
print("began")
gestureTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: { (timer) in
print("do something")
})

case .cancelled, .ended:
print("other")
if let timer = gestureTimer {
timer.invalidate()
gestureTimer = nil
}
default:
print("default")
}
}

WKInterface button doesn't change title

With WatchKit, if a user interface element isn't currently visible, it cannot be updated. So, if you've presented another interface controller "on top", you can't update any of the presenting controller's interface elements until you've dismissed the presented controller. At that point, you can safely update the presenting controller in its willActivate method.

SushiGrass' method of passing blocks is certainly one valid approach. In my testing, however, I ended up having to manage multiple blocks, and many of the subsequent blocks reversed what earlier queued blocks had accomplished (for example, first changing a label's text to "foo", then "bar", then "foo" again. While this can work, it isn't optimal.

I'd suggest that anyone who is working on a WatchKit app takes a moment to consider how they want to account for off-screen (i.e. not-currently-visible) interface elements. willActivate is your friend, and coming up with a way to manage updates in that method is worthwhile if you're moving from controller to controller.

For what it's worth, I've encapsulated a lot of this logic in a JBInterfaceController subclass that handles a lot of this for you. By using this as a base class for your own interface controller, you can simply update your elements in the added didUpdateInterface method. Unfortunately, I haven't yet had the time to write proper documentation, but the header files and sample project should get you going: https://github.com/mikeswanson/JBInterfaceController

Create WKInterfaceButton with image and left aligned text

While i haven't tried this myself i think what you're looking for to match the watch settings is a table view. Define a table view in the interface controller of your story board and add your image and label to the row(s). There's an example of how to do this in the programming guide for apple kit.



Related Topics



Leave a reply



Submit