我使用外部委托文件来处理所有UICollectionView的处理,并且我努力让集合视图单元基于所选单元格通过委托文件执行segue
这是我目前在委托文件中的内容:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
let mainController = MainController()
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
}
在主控制器上我有:
override func performSegue(withIdentifier identifier: String, sender: Any?) {
if identifier == "detailSegue" {
let detailController = DetailController()
self.present(detailController, animated: true)
}
}
我得到的错误:
Warning: Attempt to present <DetailController: 0x7fbed8e6e4b0> on <MainController: 0x7fbedda79830> whose view is not in the window hierarchy!
我以为我可以通过委托来调用引用,它会显示控制器。
由于
答案 0 :(得分:1)
您试图获得的MainController
的引用是错误的。
let mainController = MainController()
这不会为您提供对象的加载/预览实例而不是它将为您创建MainController的新对象,而不是在预览层次结构中。所以你需要一个实际的预览实例。
<强>解决方案强> 在委托类中创建UIViewController类型的全局对象,并传递预览类引用,以便在需要时使用它。
实施例
class DelegateDataSource: NSObject {
var viewController: UIViewController?
//Other Methods/Objects that you need.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
if let mainController = viewController as? MainController {
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
}
}
}
class MainController: UIViewController {
var delegateDataSource: DelegateDataSource?
func initializeDelegates() {
//Initialize you datasource object and do some tasks that you need and then assign your current instance to this class like this.
delegateDataSource.viewController = self
}
}
答案 1 :(得分:0)
static void Main(string[] args)
{
Console.WriteLine(SixDigitPalindromeWithDivisor().ToString());
Console.ReadLine();
}
public static int SixDigitPalindromeWithDivisor()
{
for (int i = 999; i > 100; i--)
{
//generating palindrome number from max
var palindromeInt = Int32.Parse(Convert.ToString(i) + ReverseString(Convert.ToString(i)));
//checking three digit number factor exist
if (CheckThreeDigitDivisorExists(palindromeInt))
{
return palindromeInt;
}
}
return 0;
}
public static bool CheckThreeDigitDivisorExists(int number)
{
for (int i = 999; i > 100; i--)
{
if (number % i == 0)
{
if (number / i <= 999)
{
return true;
}
}
}
return false;
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
这不起作用,使用委托模式或使用单一模式为您的MainController。
答案 2 :(得分:-1)
只有已经呈现的控制器才能显示另一个控制器。实施Traceback (most recent call last):
File "<input>", line 2, in <module>
File "/home/vincent/anaconda3/envs/my_env/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
的班级是didSelectItemAt
吗?如果是这样,为什么不这样做:
UIViewController