2017-08-03 1 views
-1

Nachdem der Benutzer die Einstellung der Benachrichtigung auf AlertController akzeptiert hat, muss ich UIButton in der ausgewählten TableViewCell setzen, wenn das Hinzufügen der Benachrichtigungsanforderung erfolgreich abgeschlossen wird. Aber darauf reagiert es nicht, obwohl der Code dort ankommt. Ich denke, das Problem ist, dass die Schließung des Handler-Ereignisses sich von der Schließung der Hauptfunktion unterscheidet und deshalb das UIButton-Objekt nicht korrekt innerhalb des Handler-Ereignisses kommt. Wie kann ich dieses Problem angehen? Hier ist das Codestück.Wie setze ich die iSelected-Eigenschaft von UIButton innerhalb eines anderen Abschlusses in Swift 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    var isAlarmSet=false 
    let center = UNUserNotificationCenter.current() 
    tableView.deselectRow(at: indexPath, animated: true) 

    if let alarmButton = tableView.cellForRow(at: indexPath) as? BroadcastViewCell{ 
     var row=indexPath.row 
     isAlarmSet = alarmButton.notifyButton.isSelected 
     self.alarmButton=alarmButton.notifyButton 
     if !isAlarmSet 
     { 
      //Calculating the date variable for notification time in this section i dont put it in here because it is too long. 

       if (date?.timeIntervalSinceNow.sign == .minus) 
       { 
        let alertController=UIAlertController(title:"UYARI" , message: "Programın süresi geçmiştir.", preferredStyle: .alert) 
        let cancelAction = UIAlertAction(title: "Tamam", style: .cancel) { (action:UIAlertAction!) in 

        } 
        alertController.addAction(cancelAction) 
        self.present(alertController, animated: true, completion: nil) 

       } 
       else 
       { 
        let alertController=UIAlertController(title:show.programName , message: "Program başlamadan 10 dakika önce uyarı alacaksınız.", preferredStyle: .alert) 
        let cancelAction = UIAlertAction(title: "İptal", style: .cancel) { (action:UIAlertAction!) in 

        } 
        let acceptAction=UIAlertAction(title: "Tamam" , style: .default, handler: { (action:UIAlertAction) in 

         if #available(iOS 10.0, *) 
         { 
          let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date!) 

          let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) 

          //Set the request for the notification from the above 
          let request = UNNotificationRequest(
           identifier: (show.airDate)!+(show.airTime)!, 
           content: content, 
           trigger: trigger 
          ) 

          UNUserNotificationCenter.current().add(request) 
          { (error:Error?) in 
           if error != nil 
           { 
            print(error?.localizedDescription) 
            return 
           } 
           print("Notification Register Success") 
           alarmButton.notifyButton.isSelected=true 
           UNUserNotificationCenter.current().getPendingNotificationRequests 
           { (requests) in 
             self.notifications=requests 
           } 

          } 
         } 

        alertController.addAction(cancelAction) 
        alertController.addAction(acceptAction) 
        self.present(alertController, animated: true, completion: nil) 
       } 

      } 
     } 
    } 

} 
+1

Versuch verwenden, um Daten nach der Veranstaltung erneut zu laden –

+0

auftreten Meinen Sie tableView.reloadData()? Es reagiert auch nicht – umutg4d

+1

Versuchen Sie, diese Eigenschaft in 'DispatchQueue.main.async {AlarmButton.notifyButton.isSelected = True}' –

Antwort

1

UI kann nur auf Hauptthread aktualisiert werden, dann

DispatchQueue.main.async { 
    alarmButton.notifyButton.isSelected=true 
} 
Verwandte Themen