Wat is een startcode die ik zou kunnen gebruiken om een eenvoudige UIAlertView te maken met één “OK”-knop erop?
Antwoord 1, autoriteit 100%
Als je wilt dat de melding wordt weergegeven, doe je dit:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL"
message:@"Dee dee doo doo."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// If you're not using ARC, you will need to release the alert view.
// [alert release];
Als je iets wilt doen wanneer op de knop wordt geklikt, implementeer dan deze gedelegeerde methode:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
}
}
En zorg ervoor dat uw gemachtigde voldoet aan het UIAlertViewDelegate
-protocol:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
Antwoord 2, autoriteit 32%
Andere antwoorden geven al informatie voor iOS 7 en ouder, maar UIAlertView
is verouderd in iOS 8.
In iOS 8+ moet u UIAlertController
gebruiken. Het is een vervanging voor zowel UIAlertView
als UIActionSheet
. Documentatie: UIAlertController Class Reference. En een mooi artikel over NSHipster.
Als u een eenvoudige waarschuwingsweergave wilt maken, kunt u het volgende doen:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
Snel 3 / 4/5:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: "OK",
style: .default,
handler: nil) //You can use a block here to handle a press on this button
alertController.addAction(actionOk)
self.present(alertController, animated: true, completion: nil)
Houd er rekening mee dat, aangezien deze is toegevoegd in iOS 8, deze code niet werkt op iOS 7 en ouder. Dus helaas moeten we voorlopig versiecontroles als volgt gebruiken:
NSString *alertTitle = @"Title";
NSString *alertMessage = @"Message";
NSString *alertOkButtonText = @"Ok";
if (@available(iOS 8, *)) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:alertOkButtonText, nil];
[alertView show];
}
else {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
style:UIAlertActionStyleDefault
handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
}
Snel 3 / 4/5:
let alertTitle = "Title"
let alertMessage = "Message"
let alertOkButtonText = "Ok"
if #available(iOS 8, *) {
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: alertOkButtonText,
style: .default,
handler: nil) //You can use a block here to handle a press on this button
alertController.addAction(actionOk)
self.present(alertController, animated: true, completion: nil)
}
else {
let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
alertView.show()
}
UPD: bijgewerkt voor Swift 5. Verouderde aanwezigheidscontrole van klassen vervangen door beschikbaarheidscontrole in Obj-C.
Antwoord 3, autoriteit 4%
UIAlertView is verouderd op iOS 8. Daarom wordt het aanbevolen om UIAlertController te gebruiken om een waarschuwing te maken op iOS 8 en hoger:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// Enter code here
}];
[alert addAction:defaultAction];
// Present action where needed
[self presentViewController:alert animated:YES completion:nil];
Dit is hoe ik het heb geïmplementeerd.
Antwoord 4, autoriteit 4%
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:nil //or self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
Antwoord 5, autoriteit 4%
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok",nil];
[myAlert show];
Antwoord 6, autoriteit 4%
Als aanvulling op de twee vorige antwoorden (van gebruiker “sudo rm -rf” en “Evan Mulawski”), als u niets wilt doen wanneer op uw waarschuwingsweergave wordt geklikt, kunt u gewoon toewijzen, tonen en laat het los. U hoeft het deelnemersprotocol niet te declareren.
Antwoord 7
Hier is een complete methode die maar één knop heeft, een ‘ok’, om de UIAlert te sluiten:
- (void) myAlert: (NSString*)errorMessage
{
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:errorMessage
message:@""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"ok", nil];
myAlert.cancelButtonIndex = -1;
[myAlert setTag:1000];
[myAlert show];
}
Antwoord 8
Deze paginalaat zien hoe u een UIAlertController toevoegt als u Swift gebruikt .
Antwoord 9
Eenvoudige waarschuwing met arraygegevens:
NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"Name"];
NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"message"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
message:msg
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];