码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发技巧 - 使用UISegmentedControl来对信息进行分组

时间:2016-08-15 18:41:16      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

 

 

技术分享

 

(Swift)

import UIKit
class ViewController: UIViewController {
    var segmentedControl:UISegmentedControl!
    
    override func 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"

@interface ViewController ()

@property (nonatomic, strong) UISegmentedControl *mySegmentedControl;

@end

@implementation ViewController
...
- (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:UIControlEventValueChanged];
}

- (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",
    UIImage(named: "blueDot")!,
    "Green",
    "Yellow")
segmentedControl = 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开发技巧 - 使用UISegmentedControl来对信息进行分组

标签:

原文地址:http://www.cnblogs.com/davidgu/p/5773854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!