2016-11-19 1 views
0

버튼을 누르면 버튼의 텍스트를 변경하려고하지만 작동하지 않습니다. 내가 놓친 게 있니?WKInterfaceButton의 제목을 설정하는 방법은 무엇입니까?

저는 몇 시간 동안 제 문제를 파악하려고 노력해 왔습니다. 도움을 주시면 감사하겠습니다.

h. 파일

#import <WatchKit/WatchKit.h> 
#import <Foundation/Foundation.h> 

@interface InterfaceController : WKInterfaceController 
{ 

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

m. 그것은 스위프트-3 엑스 코드 8.1에서 잘 작동

#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"]; 

} 

답변

0

파일.

// InterfaceController.swift // WatchKit 확장 수입 WatchKit 수입 재단 수입 WatchConnectivity

클래스 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

관련 문제