首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置WKInterfaceButton的标题?

如何设置WKInterfaceButton的标题?
EN

Stack Overflow用户
提问于 2016-11-19 14:05:14
回答 1查看 245关注 0票数 0

我试图改变一个按钮的文本,一旦它被按下,但它不起作用。我是不是遗漏了什么?

我已经试着解决我的问题好几个小时了。任何帮助都将不胜感激。

h.文件

代码语言:javascript
复制
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

@interface InterfaceController : WKInterfaceController
{

    IBOutlet WKInterfaceButton*playpausebtn;
}
-(IBAction)play;
@end

m.文件

代码语言:javascript
复制
#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"];

}
EN

回答 1

Stack Overflow用户

发布于 2016-11-25 21:04:16

在Swift-3 Xcode-8.1中运行良好。

// InterfaceController.swift // WatchKit扩展导入WatchKit导入基础导入WatchConnectivity

类InterfaceController: WKInterfaceController,WCSessionDelegate {

代码语言:javascript
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40689768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档