簡體   English   中英

以編程方式更改UIButton的標題在iOS Xcode-Beta 3中不起作用

[英]Programmatically change title of UIButton not working in iOS Xcode-Beta 3

嘿,我正在開發一個小應用程序,它根據日期選擇器的當前狀態更改UIButton的文本。 它要么顯示“它是哪一天?” 或“它會是哪一天?”。 但是對於xcode 6.3 beta,UIButton SetTitle方法似乎不存在。 相反,我嘗試將一個字符串分配給titleLabel.txt,但這不會更改按鈕文本。

方法如下:

(IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now =  [NSDate date];

if(currentDate<now){
    self.whatButton.titleLabel.text = @"What day was it?";
}
else {
    self.whatButton.titleLabel.text = @"What day will it be?";
}

我已經看到setTitle方法是一個可能的解決方案,但發現庫可能已被更改。 任何建議將不勝感激!

您應該像這樣設置標題:

[self.whatButton setTitle:@"What day was it?" forState:UIControlStateNormal];

使用以下代碼將按鈕的標題設置為不同的狀態,就像正常一樣

 [self.yourButton setTitle:@"Default Title" forState:UIControlStateNormal];

就好像你已經點擊按鈕選擇狀態一樣

[self.yourButton setTitle:@"Selected Title" forState:UIControlStateSelected];

就像你再次點擊你的按鈕取消選擇狀態一樣

[self.yourButton setTitle:@"Selected Title" forState:UIControlStateDisable];

以及許多其他狀態,您可以在按鈕的每個狀態中設置不同的文本

嘗試更改日期比較的方式 -

-(IBAction)changeButton:(id)sender {

NSDate *currentDate = [self.datePicker date];
NSDate *now =  [NSDate date];

NSString *titleString = ([currentDate timeIntervalSinceReferenceDate] < [now timeIntervalSinceReferenceDate]) ? @"What day was it?" : @"What day will it be?";
[self.whatButton setTitle:titleString forState:UIControlStateNormal];
}

謝謝你的所有建議。 第一個建議是有點偏離基礎,因為它不是我想改變的發送者頭銜。 第二個建議是我將來會使用的好主意。 我最終用這個解決了這個謎語:

- (IBAction) changeButton:(id)sender {
    NSDate *currentDate = [self.datePicker date];
    NSDate *now =  [NSDate date];
    UIButton *tempBtn = (UIButton *)self.whatButton;
    UIControlState state = tempBtn.state;
    if([currentDate compare:now] == NSOrderedAscending){
        [tempBtn setTitle:@"What day was it?" forState:state];
    }
    else {
        [tempBtn setTitle:@"What day will it be?" forState:state];
    }

}`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM