iOS Development Tips-use Uisegmentedcontrol to group information

Source: Internet
Author: User

(Swift)

Import UIKitclassViewcontroller:uiviewcontroller {var segmentedcontrol:uisegmentedcontrol!Overridefunc viewdidload () {super.viewdidload () let segments= [            "IPhone",            "IPad",            "IPod",            "IMac"] Segmentedcontrol=Uisegmentedcontrol (items:segments) Segmentedcontrol.center=View.center Segmentedcontrol.addtarget (Self, Action:"segmentedcontrolvaluechanged:", forControlEvents:. valuechanged) Self.view.addSubview (Segmentedcontrol)} func segmentedcontrolvaluechanged (sender: Uisegmentedcontrol) {Let Selectedsegmentindex=Sender.selectedsegmentindex let Selectedsegmenttext=Sender.titleforsegmentatindex (Selectedsegmentindex) println ("Segment \ (selectedsegmentindex) with text"+"of \ (selectedsegmenttext) is selected")    }}

(OBJECTIVE-C)

#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic, strong) Uisegmentedcontrol*Mysegmentedcontrol;@end@implementationViewcontroller ...- (void) viewdidload {[Super viewdidload]; Nsarray*segments =[[Nsarray alloc] Initwithobjects:@"IPhone",        @"IPad",        @"IPod",        @"IMac", nil]; Self.mysegmentedcontrol=[[Uisegmentedcontrol alloc] initwithitems:segments]; Self.mySegmentedControl.center=Self.view.center;        [Self.view AddSubview:self.mySegmentedControl]; [Self.mysegmentedcontrol addtarget:self Action: @selector (segmentchanged:) forcontrolevents:uicontroleventval Uechanged];}- (void) segmentchanged: (Uisegmentedcontrol *) Paramsender {if([Paramsender IsEqual:self.mySegmentedControl]) {Nsinteger Selectedsegmentindex=[Paramsender Selectedsegmentindex]; NSString*selectedsegmenttext =[Paramsender Titleforsegmentatindex:selectedsegmentindex]; NSLog (@"Segment%ld with%@ text is selected",            (Long) Selectedsegmentindex, Selectedsegmenttext); }}

(Swift)

Let segments = Nsarray (objects:    "Red",    "BlueDot ")! ,     " Green " ,     " Yellow "  = Uisegmentedcontrol (items:segments)

(OBJECTIVE-C)

Nsarray *segments = @[    @"IPhone", [UIImage imagenamed:@"IPad"],    @"IPod",    @"IMac",    ]; Self.mysegmentedcontrol=[[Uisegmentedcontrol alloc] initwithitems:segments]; CGRect Segmentedframe=Self.mysegmentedcontrol.frame;segmentedframe.size.height=128.0f; SegmentedFrame.size.width=300.0f; Self.mySegmentedControl.frame=Segmentedframe;self.mysegmentedcontrol.center=Self.view.center; [Self.view AddSubview:self.mySegmentedControl];

iOS Development Tips-use Uisegmentedcontrol to group information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.