【问题标题】:Unable to alter UIButton in EKEvent code无法更改 EKEvent 代码中的 UIButton
【发布时间】:2013-08-07 20:40:00
【问题描述】:

我正在尝试创建一个按钮,按下该按钮会创建一个日历事件,并将所述按钮的标题更改为“已创建事件”,或者可能会创建一个 AlertView 以达到相同的效果。到目前为止,这是我的代码:

- (IBAction)AddToCal:(id)sender {
EKEventStore *store = [[EKEventStore alloc] init];

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) {
       //Code to handle not-granted
    }
    else {
            //Code to create event          
            [event setCalendar:[store defaultCalendarForNewEvents]];
            NSError *err;
            [store saveEvent:event span:EKSpanThisEvent error:&err];
            [[NSUserDefaults standardUserDefaults] setBool:true forKey:[NSString stringWithFormat:@"%@Sub", PrevDest] ];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [AddReminder  setTitle: [NSString stringWithFormat:@"Subscribed!"] forState: UIControlStateNormal];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event Added!"
                                                            message:@""
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
          }
    }];
    //Where I tested different placement of the code
}

现在这会创建一个很好的事件,但它不会创建一个 AlertView 或更改标题作为确认,甚至更多,它使我的应用程序冻结并且对触摸没有响应......直到,也就是说,我按下主页按钮,然后重新打开应用程序,此时标题更改,我的 AlertView 弹出。

我尝试将标题更改代码放在标记的位置,这使它可以工作,但这当然在 if(!granted) 之外,因此无论如何都会导致按钮更改。甚至将代码放在那里也会导致同样的冻结现象。

我真的不知道这是怎么回事。有没有人遇到过同样的问题?

【问题讨论】:

  • 添加断点以查看是否已创建警报视图。
  • 我在更改标题处放置了三个中断,创建警报和显示警报。程序在所有三个地方都停止了,并且警报不是零,但是它仍然没有显示,并且按钮没有改变。

标签: ios cocoa-touch uibutton ekevent


【解决方案1】:

试试:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event Added!"
                                                        message:@""
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
       [alert show];
});

【讨论】:

  • 我明白了。我假设您还需要在主线程中更改 UIButton 标题?一旦我添加了我的 setTitle: 代码,一切都很完美。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多