UIViewController ที่นำเสนอไม่สามารถนำเสนอ UIAlertController ได้

เมื่อคลิกปุ่มส่งในแอปพลิเคชันของฉัน SubmitViewController จะถูกนำเสนอเหนือตัวควบคุมมุมมองที่ใช้งานอยู่เพื่อแสดงความคืบหน้าในการอัปโหลด

เมื่อได้รับการแจ้งเตือนข้อผิดพลาด ควรแสดงการแจ้งเตือนพร้อมข้อผิดพลาดและปิดตัวเองไป

ปัญหาคือไม่มีการแสดงการแจ้งเตือนเนื่องจาก self.navigationController เท่ากับ nil ฉันจะนำเสนอ Alert ในกรณีนั้นได้อย่างไร?

ฉันไม่สามารถใช้กระดานเรื่องราวเพื่อสร้างอินสแตนซ์ SubmitViewController ตามที่บางคนแนะนำ เนื่องจากไม่ได้เป็นส่วนหนึ่งของกระดานเรื่องราว

ดูตัวควบคุมที่ใช้ในการนำเสนอ SubmitViewController:

-(void)submitBtnClicked:(id)sender {
    SubmitViewController *submitViewController = [SubmitViewController new];
    [self.navigationController presentViewController:submitViewController animated:YES completion:nil];
}

SubmitViewController.swift:

@objc func submitErrorNotification(_ notification:Notification) {
    self.unsubscribe()

    let title:String = notification.userInfo!["title"] as! String
    let message:String = notification.userInfo!["message"] as! String

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

    if (self.navigationController == nil) {
        NSLog("Error: navigation controller is nil");// THIS error occurs
    }

    self.navigationController?.present(alertController, animated: false, completion: nil)

    self.dismiss(animated: false, completion: nil)
}

comment
เหตุใดคุณจึงต้องการนำเสนอตัวควบคุมการแจ้งเตือนบนตัวควบคุมการนำทาง ทำไมคุณไม่เพียงแค่นำเสนอมันบนอินสแตนซ์ SubmitViewController ของคุณและยกเลิกสิ่งนั้นในตัวจัดการการดำเนินการ   -  person Paulw11    schedule 02.10.2018
comment
@ Paulw11 นั่นคือวิธีแก้ปัญหาของฉันตอนนี้ ฉันต้องนำเสนอมันบนตัวควบคุมการนำทางเพื่อให้สามารถยกเลิกตัวควบคุมมุมมองที่ซ่อนอยู่และแทนที่ด้วยตัวควบคุมมุมมองอื่น   -  person Peter G.    schedule 02.10.2018
comment
จากนั้นคุณควรใช้รูปแบบการมอบหมายเพื่อให้ตัวควบคุมมุมมองการส่งสามารถบอกผู้รับมอบสิทธิ์ว่ามีข้อผิดพลาดและผู้รับมอบสิทธิ์สามารถตัดสินใจว่าจะทำอย่างไร   -  person Paulw11    schedule 02.10.2018


คำตอบ (1)


เหตุใดคุณจึงใช้คำสำคัญแจ้งเตือนใน:

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

ลองสิ่งนี้:

SubmitViewController *myViewController = [SubmitViewController new];
    [self.navigationController presentViewController:myViewController animated:YES completion:nil];
person Mahesh Shahane    schedule 02.10.2018
comment
แค่ไม่ได้ตั้งใจ ไม่เกี่ยวอะไรกับปัญหา - person Peter G.; 02.10.2018