简体   繁体   中英

iOS: Warnings when handling didSelectViewController

The following code:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController == [tabBarController.viewControllers objectAtIndex:0]) {
        MySearchViewController *controller = viewController;
        [[controller tableView] reloadData];
    }
}

Yields the following warning:

Incompatible pointer types initializing 'MySearchViewController *__strong' with an expression of type 'UIViewController *__strong'

What would be the proper way to cast viewController into the proper class MySearchViewController to call its method?

Just change to this:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController == [tabBarController.viewControllers objectAtIndex:0] && [viewController isKindOfClass:[MySearchViewController class]]) {
        MySearchViewController *controller = (MySearchViewController *)viewController;
        [[controller tableView] reloadData];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM