简体   繁体   English

WKInterface按钮不会更改标题

[英]WKInterface button doesn't change title

I'm trying to change the title of a button after I call back from a notification but it doesn't respond at all. 我在通知回来之后尝试更改按钮的标题,但它根本没有响应。 I checked it's not nil and checked the text Im' assigning and all is good. 我检查了它不是零并检查我分配的文本,一切都很好。 I made the property type strong instead of weak but no success. 我使房产类型strong而不是weak但没有成功。

- (void) setButtonTitleFromSelectedSearchResult:(NSNotification *)notif
{

    [self popController];

    self.sourceMapItem = [[notif userInfo] valueForKey:@"SelectedResult"];

    NSLog(@"The Selected Result is: %@", self.sourceMapItem.name);

    //Testing
    NSLog(@"%@", self.fromButton); // check it's not nil

    [self.fromButton setTitle:self.sourceMapItem.name];
}

With WatchKit, if a user interface element isn't currently visible, it cannot be updated. 使用WatchKit,如果用户界面元素当前不可见,则无法更新。 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. 此时,您可以使用willActivate方法安全地更新呈现控制器。

SushiGrass' method of passing blocks is certainly one valid approach. SushiGrass传递积木的方法当然是一种有效的方法。 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. 然而,在我的测试中,我最终不得不管理多个块,并且许多后续块反转了先前排队的块已完成(例如,首先将标签的文本更改为“foo”,然后是“bar”,然后是“foo” “再说一遍。虽然这可行,但并非最佳。

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 (ie not-currently-visible) interface elements. 我建议任何正在使用WatchKit应用程序的人花点时间考虑他们想要如何考虑屏幕外(即当前不可见的)界面元素。 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. willActivate是你的朋友,如果你从控制器转移到控制器,那么提出一种管理该方法更新的方法是值得的。

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. 为了它的价值,我在JBInterfaceController子类中封装了很多这样的逻辑,它为你处理了很多这样的事情。 By using this as a base class for your own interface controller, you can simply update your elements in the added didUpdateInterface method. 通过将此作为您自己的接口控制器的基类,您可以在添加的didUpdateInterface方法中更新元素。 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 不幸的是,我还没有时间编写适当的文档,但头文件和示例项目应该让你去: https//github.com/mikeswanson/JBInterfaceController

I'm using latest XCode 6.3 and below code working with me. 我正在使用最新的XCode 6.3及以下代码。 self.testBtn is bind with Storyboard and its WKInterfaceButton self.testBtn与Storyboard及其WKInterfaceButton绑定

I also have attached screenshot with affected result. 我还附上了受影响结果的屏幕截图。

I'm setting initial text in - (void)willActivate 我正在设置初始文本- (void)willActivate

- (void)willActivate {
    [super willActivate];
    [self.testBtn setTitle:@"Test"];
    [self performSelector:@selector(justDelayed) withObject:nil afterDelay:5.0]
}

-(void)justDelayed
{
    [self.testBtn setTitle:@"Testing completed...!!"];
}

在此输入图像描述

在此输入图像描述

If you're using an IBOutlet for the property fromButton be sure that is connected to WKInteface on the storyboard, like below: 如果您正在使用IBOutlet作为属性fromButton,请确保它连接到故事板上的WKInteface,如下所示:

IBOutlet中

I solved this kind of issue by creating a model object that has a property that is a block of type () -> (Void) (in swift). 我通过创建一个模型对象来解决这类问题,该模型对象具有一个类型为() -> (Void)的块属性(在swift中)。 I create the model object, set the action in the block that I'd like the pushing WKInterfaceController to do on completion, and finally pass that model object in the context to the pushed WKInterfaceController . 我创建了模型对象,在我想要推动WKInterfaceController在完成时执行的块中设置动作,最后将上下文中的模型对象传递给推送的WKInterfaceController The pushed WKInterfaceController holds a reference to the model object as a property and calls it's completion block when it's done with whatever it needs to do and after func popController() . 推送的WKInterfaceController将模型对象的引用作为属性保存,并在完成任何需要的操作并在func popController()之后调用它的完成块。

This worked for me for patterns like what you are describing along with removing rows on detail controller deletion, network calls, location fetches and other tasks. 这对我来说非常适用于您所描述的模式以及删除详细控制器删除,网络调用,位置提取和其他任务的行。

You can see what I'm talking about here: https://gist.github.com/jacobvanorder/9bf5ada8a7ce93317170 你可以在这里看到我在说什么: https//gist.github.com/jacobvanorder/9bf5ada8a7ce93317170

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM