Существует несколько способов протестировать тип объекта:
1) Использование эти оператор:
if (anObject is MyType) {
// anObject is MyType or a derived class
...
}
2) Использование как оператор:
MyType newObject = anObject as MyType;
if (newObject != null ) {
// newObject is anObject cast to MyType
...
}
3) Использование typeof () и GetType () [3 изменения]:
// #1
if (typeof(MyType) == anObject.GetType()) {
// anObject is a MyType
...
}
//#2
public static bool IsType(object obj, string type)
{// modified from Visual C# 2005 Recipes {Apress}
// Get the named type, use case-insensitive search, throw
// an exception if the type is not found.
Type t = Type.GetType(type, true, true);
return t == obj.GetType();
}
//#3
public static bool IsTypeOrSubclass(object obj, string type)
{// modified from Visual C# 2005 Recipes {Apress}
// Get the named type, use case-insensitive search, throw
// an exception if the type is not found.
Type t = Type.GetType(type, true, true);
return t == obj.GetType() || obj.GetType().IsSubclassOf(t);
}
Прежде всего, когда вы поместите этот код в applicationDidFinishLaunching , может случиться так, что контроллеры, экземпляры которых созданы из Interface Builder, еще не связаны с вашим приложением (так что «красный» и «синий» по-прежнему nil
).
Но чтобы ответить на ваш первоначальный вопрос, что вы делаете неправильно, потому что вызываете dismissModalViewControllerAnimated:
не на том контроллере! Это должно быть так:
[blue presentModalViewController:red animated:YES];
[red dismissModalViewControllerAnimated:YES];
Обычно «красный» контроллер должен решить уволиться в какой-то момент (возможно, когда нажата кнопка «отменить»). Тогда "