我想创建一个自定义UIView
类,根据某些输入将显示动态数量的UISegmentedControl
对象。例如,如果客户的购物车中有5件商品,则UIView
应生成5个UISegmentedControl
对象,然后我将与每件商品链接。iOS:在UIView中动态创建UISegmentedControl
我遇到的问题是让此工作在UIView
。这是我迄今为止所做的。我成功地创建了一个UISegmentedControl
对象并以编程方式将其显示在我的主要UIViewController
中。将它添加到我的UIView
课程时,我没有任何显示。下面是UIView
类的实现代码:
#import "ajdSegmentView.h"
@implementation ajdSegmentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *itemArray = [NSArray arrayWithObjects:@"Yes", @"No", nil];
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:itemArray];
button.frame = CGRectMake(35,44, 120,44);
button.segmentedControlStyle = UISegmentedControlStylePlain;
button.selectedSegmentIndex = 1;
[self addSubview:button];
}
return self;
}
@end
我通过故事板创建了一个新UIView
对象,并把它放在UIViewController
场景里面。我确保将类从通用UIView
类设置为我的新自定义类。我在UIViewController
课中增加了UIView
的插座。这里是UIViewController
里面执行代码:
#import "ajdViewController.h"
@interface ajdViewController()
@end
@implementation ajdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.segmentView = [[ajdSegmentView alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这就是我都试过了。我一直在寻找很多页面,并试图在不询问这里的情况下实现这一点,但我似乎正在寻找错误的地方。
您是否在调试器中验证过'initWithFrame:'被调用? –