I am new to iOS, and I am trying to create a tab view with a UITabBarController. Everything works fine still the app loads with the first tab, but when I clicked the second tab item app hangs with the exception,

   Terminating app due to uncaught exception `NSInvalidArgumentException`, reason: '-[UIPeripheralHost _tabBarItemClicked:]: unrecognised selector sent to instance 0x8c692a0"

Here is my code:

- (void) setupview
{
FirstViewController *first = [[FirstViewController alloc]init];
SecondViewController *second = [[SecondViewController alloc] init];

first.title = @"First";
second.title = @"Second";

first.tabBarItem.image = [UIImage imageNamed:@"icon1.png"];
second.tabBarItem.image = [UIImage imageNamed:@"icon2.png"];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 50, 320, 410);

[tabBarController setViewControllers:[NSArray arrayWithObjects:first, second, nil]];

[self.view addSubview:tabBarController.view];

}
有帮助吗?

解决方案

The problem is that the tabBarController is not retained (and that is why the target in the exception is a UIPeripheralHost [garbage]).

So in order to keep the controller alive you will have to hold a strong reference to it via a strong property or add it as a child controller (via addChildViewController:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top