如何在选择不同的段时使用 UISegmentedControl 加载不同的 subview ?我是 Objective-C 和 iOS 编程的新手。
或者有没有办法让 UITabBarController 看起来像 UISegmentedControl?
最佳答案
对于程序化方法
在加载 View 中:
{
NSArray *segments = [NSArray arrayWithObjects:@"Left", @"Right", nil];
segmentedControl = [[UISegmentedControl alloc]initWithItems:segments];
[segmentedControl addTarget:self
action:@selector(changeSubViews)
forControlEvents:UIControlEventValueChanged];
contentView = [UIView alloc]initwithFrame:(the frame where you want the subViews to be displayed)];
[self.view addSubView:contentView];
}
- (void)changeSubViews
{
switch(segmentedControl.selectedSegmentIndex)
{
case 0:
{
[rightView removeFromSuperView];
if (leftView ==nil){leftView alloc, init;}
[contentView addSubView:leftView];
break;
}
case 1:
{
[leftView removeFromSuperView];
if (rightView ==nil){rightView alloc, init;}
[contentView addSubView:rightView];
break;
}
}
}
关于iphone - 如何使 UISegmentedControl 像 UITabBarController 一样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4524103/