2015-04-16 21 views
11

Come visualizzare Avviso su Apple Watch. C'è qualche alternativa per mostrare gli avvisi in Apple Watch perché ho controllato e UIAlertView non funziona su Apple Watch.Come mostrare avviso su Apple Watch

+0

Aggiornato la mia risposta. Con watchOS2 credo che tu abbia una soluzione migliore per quello che volevi :) –

risposta

13

Con watchOS2

Con watchOS2 è possibile utilizzare il metodo WKAlertAction:

+ (instancetype nonnull)actionWithTitle:(NSString * nonnull)title 
           style:(WKAlertActionStyle)style 
           handler:(WKAlertActionHandler nonnull)handler 

Con watchOS1

Se non ti dispiace perdere la caratteristica di un UIAlertView di vedere il contenuto dietro, è possibile:

1 - Creazione di un ErrorInterfaceController (con o senza un pulsante OK)

enter image description here

2 - Impostare l'identificativo per "ErrorInterfaceController"

enter image description here

3 - Presente quell'errore con:

[self presentControllerWithName:@"ErrorInterfaceController" 
         context:@{@"title" : @"yourTitle", 
            @"text" : @"yourText"}]; 

4 - In ErrorInterfaceController.m è possibile impostare il titolo e il testo con il co ntext.

Nota che il tuo ErrorInterfaceController può avere un titolo che è vuoto e il pulsante ok può chiuderlo oppure puoi lasciare il modo in cui è con un predefinito "Fatto".

Questa è la soluzione più semplice per presentare un messaggio.

Se si desidera qualcosa di più complesso, è necessario ricordare che WatchKit non ha uno z-index e non è possibile aggiungere elementi dinamicamente in base al codice. Pertanto, è necessario disporre di una soluzione che utilizzi UIImages resi nell'estensione per app e inviandoli a WatchKit.

+0

plz spiegare in profondità. Dove mettere il codice? cosa fare nel controller per passare il contesto? –

+0

Completata la risposta con alcune immagini. L'ideia definisce fondamentalmente un nuovo controller di interfaccia che è responsabile di presentare un errore e quindi semplicemente presentarlo. –

3

Un'altra opzione consiste nel mettere l'interfaccia utente di avviso in un gruppo e mostrarla/nasconderla se necessario. A seconda del design della tua app, questo può funzionare abbastanza bene. Faccio qualcosa di simile per mostrare il caricamento dell'interfaccia utente.

6

Per watchos 2, ecco un esempio:

WKAlertAction *action = 
      [WKAlertAction actionWithTitle:@"OK" 
            style:WKAlertActionStyleDefault 

            handler:^{ 
             // do something after OK is clicked 
            }]; 

NSString *title = @"Oops!"; 
NSString *message = @"Here comes the error message"; 

[self.interfaceController 
      presentAlertControllerWithTitle:title 
            message:message 
          preferredStyle:WKAlertControllerStyleAlert 
            actions:@[ action ]]; 
4

In watchos 2

Objective-C

NSString *titleOfAlert = @"Something Happened Wrong"; 
NSString *messageOfAlert = @"Error Message Here"; 
[self.interfaceController presentAlertControllerWithTitle: titleOfAlert 
                message: messageOfAlert 
              preferredStyle: 
              WKAlertControllerStyleAlert 
              actions:@[ 
               [WKAlertAction actionWithTitle: @"OK" 
                  style: WKAlertActionStyleDefault 
                  handler: ^{ 
                   //something after clicking OK 
                  } 
              ]]; 

Swift

let titleOfAlert = "Something Happened Wrong" 
let messageOfAlert = "Error Message Here" 
self.interfaceController.presentAlertControllerWithTitle(titleOfAlert, message: messageOfAlert, preferredStyle: .Alert, actions: [WKAlertAction(title: "OK", style: .Default){ 
    //something after clicking OK 
}]) 

In watchos 1

si dovrebbe fare un secondo controller di interfaccia, come dice Tiago, poi presentare la seconda dalla prima:

Objective-C

[self presentControllerWithName:@"ErrorInterfaceController" 
        context:@{@"title" : @"yourTitle", 
           @"text" : @"yourText"}]; 

Swift

self.presentController(name: "ErrorInterfaceController", context:["title":"yourTitle" , "text":"yourText"]) 
0

Aggiornamento per Swift 3.0 - In watchos 3,0

let action = WKAlertAction(title: "Decline", style: WKAlertActionStyle.default) { 
     print("Ok") 
    } 
    presentAlert(withTitle: "Message", message: "Please select value. Swipe right to change it.", preferredStyle: WKAlertControllerStyle.alert, actions:[action]) 

Speranza che aiuta !!!

Problemi correlati