What do I need to replace ??? with to make it work? Thanks!
- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
switch(viewController.???) {
case 0:
// first UIViewController was selected
case 1:
/开发者_如何学编程/ second UIViewController was selected
break;
}
}
You have this a bit confused.
In
(void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController {
the viewcontroller selected is pointed to by the pointer viewController
.
Notice how it says didSelectViewController: viewController
, this is a common syntax in Objective C which indicates that the UIViewController object that was selected is pointed to by the pointer that statement. In other languages one only has to mention the type of the object coming in, in Objective C the reason for that object is part of the method name.
There is no need for a switch, nor would one work.
switch(tabBarController.selectedIndex){
case 0:
...
break;
...
}
A way to check which viewController was selected is to check out the tag which is an NSInteger property of a UIView.
So you can do...
switch(viewController.view.tag)
{
case 0:
// do work
break;
case 1:
// do work
break;
}
I think you just need this line of code:
tabBarController.selectedIndex
You can check this number to know the tab selected, hence the controller selected.
精彩评论