【问题标题】:Update titleLabel UIButton更新 titleLabel UIButton
【发布时间】:2014-01-21 15:48:31
【问题描述】:

我有一个带有 titleLabel 的 UIButton,例如,像这样:@"Download it!"

我想在下载完成后,用另一个文本更新我的按钮的titleLabel,例如:@"Already downloaded!"

我可以更改状态(启用或不启用)但无法刷新/更新 UIButton 的 titleLabel。

知道怎么做吗?我试过[myButton setNeedsDisplay];,但它不起作用。

感谢您的建议和帮助。

更新 1: 解决方案:

[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>]

【问题讨论】:

  • 你在用[button setTitle: forState:]来更新它吗?
  • +1,谢谢大家!
  • @Lapinou。 .一个建议。如果你得到了你的问题的答案,最好接受一个答案(这里的任何一个)。这样问题就不会出现在“未回答的问题”中。希望你理解..
  • @achievelimitless 您正在为 UIButton 的不同状态(如正常或突出显示)提供答案,但在下载完成时不提供答案。当按钮被按下或突出显示时,下载不需要完成。
  • @Lapinou 看看我的回答。这与 UIButtonStates 无关

标签: ios iphone objective-c uibutton uilabel


【解决方案1】:

你试过了吗?

[yourButton setTitle:<#(NSString *)#> forState:<#(UIControlState)#>]

【讨论】:

    【解决方案2】:

    您可以更改按钮标题标签中的文本。

    [aButton setTitle:@"Already downloaded!" forState:UIControlStateNormal];
    

    有关该主题的更多信息以及控件状态的完整列表,请参阅:https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/reference/reference.html#//apple_ref/doc/c_ref/UIControlState

    【讨论】:

      【解决方案3】:

      本文解释的所有示例都解释了按钮各种状态的标题更改,例如UIControlStateNormal、UIControlStateHighlighted,但在下载完成时并没有这样做。

      最简单的方法是通知您的viewController 某个过程(下载)已完成。然后根据需要更改按钮标题。

      可以试试这个代码。

      1. 在你的 ViewController viewDidLoad 中添加一个按钮和一个通知观察者

        self.someButton.title = @"Download Now"; // set the button title
        
        // Add notification Observer
        [NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(notifyDownloadComplete:) 
                                                    name:@"DOWNLOAD_COMPLETE"
                                                  object:nil];
        
      2. 现在定义观察者的目标方法来执行按钮标题更改为

        -(void)notifyDownloadComplete:(NSNotification*)note {
            self.someButton.title = @"Already Downloaded";
        }
        
      3. 现在通过 GCD 添加一个下载方法,然后在完成后发布通知。

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           //Here your non-main thread. Try Downloading something
           dispatch_async(dispatch_get_main_queue(), ^{
           //Here you returns to main thread.
           [[NSNotificationCenter defaultCenter] postNotificationName:@"DOWNLOAD_COMPLETE" 
                                                               object:nil];
               });
           });
        

      这会将self.someButton 的标题更改为您想要的任何名称,例如Already Downloaded。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2022-11-29
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        相关资源
        最近更新 更多