如何从另一个控制器调用警报功能?

时间:2015-12-10 12:34:06

标签: ios swift uitableview alert

我有一个OnePresence.swift文件,我有:

extension OnePresence: XMPPStreamDelegate {
    public func xmppStream(sender: XMPPStream, didReceivePresence presence: XMPPPresence) {
        OpenChatsTableViewController().showAlert(username)
    }
}

在我的OpenChatsTableViewController中:

func showAlert(username: String) {
    showBuddyRequest(username)
}

其中:

import Foundation

protocol BuddyRequestProtocol {
    func showBuddyRequest(fromUser: String)
}

extension BuddyRequestProtocol where Self: UIViewController {
    func showBuddyRequest(fromUser: String) {
        let alert = UIAlertController(title: "AAA", message: "\(fromUser) wants to add you as a friend.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Friend Request", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

在这种情况下,我得到了下一个错误:

Warning: Attempt to present <UIAlertController: 0x7c3c4400> on <OpenChatsTableViewController: 0x7b0ecee0> whose view is not in the window hierarchy!

如何修复此代码并重写/重构它?

1 个答案:

答案 0 :(得分:-1)

您的String albumName = albumTitle; String ExternalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/My_Albums/" + albumName; File targetDirectory = new File(ExternalStorageDirectory); if (targetDirectory != null) { File[] files = targetDirectory.listFiles(); if (files != null) { filePathString = new String[files.length]; fileNameString = new String[files.length]; for (int i = 0; i < files.length; i++) { filePathString[i] = files[i].getAbsolutePath(); System.out.println("filePAthStr: " + filePathString[i]); fileNameString[i] = files[i].getName(); System.out.println("fileNameStr: " + fileNameString[i]); customGridAdapter.add(filePathString[i]); } } } 未显示,因此无法显示提醒。在尝试显示警报之前,首先必须确保OpenChatsTableViewController处于可见状态(例如,通过将其推入导航堆栈)。