开发者

Detect if UIViewController is running inside an existing UINavigationController

开发者 https://www.devze.com 2023-03-30 01:46 出处:网络
I\'m creating a generic reusable UIViewController component that people can add to their appl开发者_运维百科ications. It requires a navigation bar at the top where it will add some buttons.

I'm creating a generic reusable UIViewController component that people can add to their appl开发者_运维百科ications. It requires a navigation bar at the top where it will add some buttons.

I can easily create a navigationBar and add the buttons, but if the developer using my component is adding the view as part of an existing navigation structure, they might end up with 2 navigation bars.

In other words, if my view is loaded with:

 [self.navigationController pushViewController:controller animated:YES];

then it should not add a navigationbar and use what's already there. If the view is loaded with:

 [self presentModalViewController:controller animated:YES];

then it should add its own navbar.

Without requiring the developer that uses my controller to do something like a useNavBar:YES, is there a way to do this automatically?

Something like a [self isRunningInsideANavigationController] or [self hasNavigationBar] would do.


You could use self.navigationController for that purpose. It will return nil or the navigationController.

0

精彩评论

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

关注公众号