I want to create a segmented control dynamically in iOS Swift.
I want to do so because I want the segmented control with different number of segments on different times.
I have found a tutorial, but unfortunately this is on Objective-C. I have zero knowledge of Objective c.
Can anybody translate this code to Swift, or provide me a tutorial link for creating a segmented control dynamically in Swift?
Here is some code that should get you started.
let items = ["one", "two", "three"]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame = CGRect(x: 10, y: 10, width: 100, height: 30)
segmentedControl.addTarget(self, action: Selector("sel:"), forControlEvents: .ValueChanged)
segmentedControl.selectedSegmentIndex = 1
view.addSubview(segmentedControl)
Here is the same in Objective-C, see the similarities?
NSArray *items = [NSArray arrayWithObjects: @"one", @"two", @"three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(10, 10, 100, 30);
[segmentedControl addTarget:self action:@selector(sel:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;
[view addSubview:segmentedControl];
You should really learn the basics of Objective-C, because most tutorials and books are written in it.