开发者

Compare self.parentViewController to a given UIViewController

开发者 https://www.devze.com 2023-03-06 04:32 出处:网络
Is it possible? That is, can I do something like this, which doesn\'t work... if (self.parentViewController == CustomViewController) {

Is it possible? That is, can I do something like this, which doesn't work...

if (self.parentViewController == CustomViewController) {
    // do something
} else {
    // do something else
}

...to make, for example, a Settings panel look more like either of its possible parent controllers? (Example again, if the menu is soft and light but the gameplay is kinda dark, it'd be convenient to say "if your parent is A look lik开发者_StackOverflow中文版e this, but if your parent is B, look like this")

If it definitely can't be done, or if comparing the parentViewController to something else is dangerous/messy, I'll just set a flag fromView and code according to that.


You could go with if([self.parentViewController isKindOfClass:[CustomViewController class]])


self.parentViewController returns NavigationController so it doesn't work, so I found other way, this worked for me:

unsigned long currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];

//previous view controller
UIViewController *view = (UIViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];

if([view isKindOfClass:[CustomViewController class]])
{
    [self runSomething];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号