Bir necha soniyadan so'ng UIAlertController xizmatidan voz kechish kerakmi?

Menga bitta quti ko'rsatilmasdan ko'rsatish kerak, biroq 3 soniyadan keyin yo'qolishi kerak. Har qanday takrorlashni qo'shish mumkinmi?

  UIAlertController * alert=   [UIAlertController
                                alertControllerWithTitle:@"Please wait, checking your PAID INVOICE, to allow the service."
                                message:"...waiting..."
                                preferredStyle:UIAlertControllerStyleAlert];

  [self.window.rootViewController presentViewController:alert animated:YES 
  completion:nil];   
6

7 javoblar

alertController bilan birga performanceSelector ni qo'shing va UIAlertController obyektini gobally

 [self performSelector:@selector(hideAlertView) withObject:nil afterDelay:3.0];

-(void)hideAlertView{
 [alert dismissViewControllerAnimated:YES completion:nil]; //or use [self dismissViewControllerAnimated:alert completion:nil];
}
5
qo'shib qo'ydi
Agar ogohlantirish allaqachon bekor qilingan bo'lsa-chi?
qo'shib qo'ydi muallif CZ54, manba

Quyidagi kod quyidagi ishni bajaradi:

UIAlertController *alert=   [UIAlertController
                                alertControllerWithTitle:@"Please wait, checking your PAID INVOICE, to allow the service."
                                message:"...waiting..."
                                preferredStyle:UIAlertControllerStyleAlert];

[self.window.rootViewController presentViewController:alert animated:YES 
  completion:nil]; 


dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [alert dismissViewControllerAnimated:YES completion:^{

            //Dismissed
        }];

});
5
qo'shib qo'ydi
u ishladi. AppDelegate.m da ogohlantirishim bor
qo'shib qo'ydi muallif YumYumYum, manba
Yaxshi g'oya, lekin agar kodni yuborish yoki chiqarib yuborish kodini presentViewController completion: blokiga qo'shish kerak bo'lsa
qo'shib qo'ydi muallif Ashley Mills, manba
funktsiya bir necha marta talab etilsa, bu kod dublikati kodiga olib keladi
qo'shib qo'ydi muallif CZ54, manba

Buni quyidagi kabi qilishingiz mumkin:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)),     dispatch_get_main_queue(), ^{
    [alert dismissViewControllerAnimated:YES completion:nil];
});
4
qo'shib qo'ydi
funktsiya bir necha marta talab etilsa, bu kod dublikati kodiga olib keladi
qo'shib qo'ydi muallif CZ54, manba

Siz bu kodni UIAlertController kodini bekor qilish uchun foydalanishingiz mumkin. Jahon kodi UIAlertController ni e'lon qilishingiz kerak.

[self performSelector:@selector(removeAlert) withObject:nil afterDelay:3];

Sizning selektsiya usuli

-(void)removeAlert{
    [self dismissViewControllerAnimated:alert completion:nil];
}
3
qo'shib qo'ydi

@ RonakChaniyara tomonidan berilgan javobdan so'ng, bu ogohlantirish hali ham taqdim etilishini (masalan, ishdan bo'shatish uchun tugmani bo'lgan ogohlantirish bilan ishlatilganda) testdan o'tkazadi.

[presentViewController:alert animated:YES completion: {

   //Dispatch 3 seconds after alert presented
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

       //Check that alert is still presented
        if self.presentedViewController == alert {
           //Dismiss if it is
            [self.dismissViewControllerAnimated:YES completion:^{    
                //Dismissed
            }];    
        }
    });
}]; 

va Swiftda ...

let alert = UIAlertController(title: "Please Wait", message: "…waiting…", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))

present(alert, animated: true) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
        guard self?.presentedViewController == alert else { return }

        self?.dismiss(animated: true, completion: nil)
    }
}
2
qo'shib qo'ydi

O'zingizning sinfingiz uchun UIAlertController tagining subklassiga va viewDidAppear ga eng yaxshi yo'l, avtomatik ravishda ishdan bo'shatish uchun NSTimer qo'shing.

Taymerni invalidate kodini unutmang va uni viewDidDisappear usulida qoldiring

1
qo'shib qo'ydi
Dahshatli fikr ... siz ommaning ishini chalkashtirasiz!
qo'shib qo'ydi muallif Ashley Mills, manba
Nima uchun? Subklassing o'ta murakkab bo'lganmi? Qayta foydalanish mumkin kod ham mavjudmi? Va sinovga layoqatimi?
qo'shib qo'ydi muallif CZ54, manba

Buni ham sinab ko'rishingiz mumkin: Objective-C uchun

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:^{
        [self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:3.0];
    }];

    - (void)dismissAlertController:(UIAlertController *)alertController {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }

Swift 3 uchun

    let alertController = UIAlertController(title: "Title", message: "message", preferredStyle: .alert)
    present(alertController, animated: true) { 
        self.perform(#selector(ViewController.dismissAlertController(alertController:)), with: alertController, afterDelay: 3.0)
    }

    internal func dismissAlertController(alertController: UIAlertController) {
        alertController.dismiss(animated: true, completion: nil)
    }
0
qo'shib qo'ydi