UIView中的UISegmentedControl不响应change事件

kal*_*ies 1 objective-c uisegmentedcontrol uiview ios ios7

我已经在iOS7 SDK上以编程方式创建了一个基本应用程序:

  1. 创建了两个UINavigationController
  2. 为每个人添加了一个视图
  3. 创建了一个包含两个项目的UITabBarController
  4. 将每个UINavigationController添加到其各自的UITabBarController中

这工作正常,一切都以编程方式完成.我的项目只包含一个简单的窗口应用程序模板,没有任何xib或故事板,一切都在我的AppDelegate.m的应用程序didFinishLaunchingWithOptions中完成.

我试图添加一个UISegmentedControl(仍以编程方式)作为第一个UINavigationController视图的子视图,如下所示:

// First navigation controller
UINavigationController *firstNavController = [[UINavigationController alloc] init];
firstNavController.navigationBar.barTintColor = [UIColor redColor];
firstNavController.title = @"First";
firstNavController.navigationBar.topItem.title = @"First top";
firstNavController.tabBarItem.image = [UIImage imageNamed:@"FirstTab"];

// First view
UIView *firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor yellowColor];

// Add a segmented control to this view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
            [NSArray arrayWithObjects:@"One", @"Two", nil]];
// Register an event handler
[segmentedControl addTarget:self action:@selector(segmentedControlSelectedIndexDidChange:)
           forControlEvents:UIControlEventValueChanged];
// Shape it and select its first item
segmentedControl.frame = CGRectMake(10, 100, 300, 20);
segmentedControl.selectedSegmentIndex = 1;

// add the segmented control to the view
[firstView addSubview:segmentedControl];

// and the view to the first tab's navigation controler
[firstNavController.view addSubview:firstView];
Run Code Online (Sandbox Code Playgroud)

UISegmentedControl会显示,但不会处理触摸事件.

如果我更改我的代码并直接在UINavigationController中添加UISegmentedControl,则触摸事件实际上正在响应:

// and the segmented control directly within the navigation controller
[firstNavController.view addSubview:segmentedControl];
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我该怎么办才能让它回应它的触摸事件?

Ash*_*ley 5

您需要设置视图的大小.

令人困惑的是一个视图将是可见的,即使它是太小,不适合它的内容,但它是只会给接触反应了过来区官方大小.