Change of buttons’ color of UISegmentedControl

I want you to choose exclusively “Month” and “All”, or “Timeline” and “Tag” in the following example. However, we cannot divide UISegmentedControl into two or more groups. I would like to express it by coloring each button.


In the state of the first stage, “Month” and “Timeline” are chosen, respectively.


If “All” is touched, the color of “Month” will disappear and a color will be attached to “All.” It is changeless to “Timeline” and “Tag.”


Furthermore, a touch of “Tag” will change the color of a button similarly.

Sample code is below.


- (void)viewWillAppear:(BOOL)animated {
    navigationSegment = [[UISegmentedControl alloc] initWithItems:nil];
    navigationSegment.segmentedControlStyle = UISegmentedControlStyleBar;
    navigationSegment.momentary = YES;
    //initialize UISegmentedControl
    [navigationSegment insertSegmentWithTitle:NSLocalizedString(@"month", @"Month")  atIndex:0 animated:NO];
    [navigationSegment insertSegmentWithTitle:NSLocalizedString(@"all", @"All")  atIndex:1 animated:NO];
    [navigationSegment insertSegmentWithTitle:NSLocalizedString(@"sorttime",@"Timeline")  atIndex:2 animated:NO];
    [navigationSegment insertSegmentWithTitle:NSLocalizedString(@"sorttag",@"Tag")  atIndex:3 animated:NO];
    [navigationSegment insertSegmentWithImage:[UIImage imageNamed: @"mail2.png"] atIndex:4 animated:NO];
    //call controlPressed when UISegmentedControl is pushed
    [navigationSegment addTarget:self action:@selector(controlPressed:) forControlEvents:UIControlEventValueChanged];

    //Initial value: Month and Timeline is ON.
    tagOrTime = TIMELINE_FLAG_ON;
    allOrMonth = MONTH_FLAG_ON;
    //When view is displayed, objectAtIndex(es) are 0, 1, 2, 3, and 4 from the left.
    [[[navigationSegment subviews] objectAtIndex:0] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];
    [[[navigationSegment subviews] objectAtIndex:1] setTintColor:nil];
    [[[navigationSegment subviews] objectAtIndex:2] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];
    [[[navigationSegment subviews] objectAtIndex:3] setTintColor:nil];


- (void)controlPressed:(id)sender{
    int index = navigationSegment.selectedSegmentIndex;
    if(index == 2){
        tagOrTime = TIMELINE_FLAG_ON;
    }else if(index == 3){
        tagOrTime = TAG_FLAG_ON;
    }else if(index == 0){
        allOrMonth = MONTH_FLAG_ON;
    }else if(index == 1){
        allOrMonth = ALL_FLAG_ON;
    }else if(index == 4){
    //Since the turn of Subview of UISegmentedControl changes irregularly, it rearranges from the left.
    NSArray *sortedViews = [navigationSegment.subviews sortedArrayUsingFunction:compareViewsByOrigin context:NULL];

    //A flag is changed according to the button pushed now.
    if (tagOrTime == TAG_FLAG_ON) {
        [[sortedViews  objectAtIndex:2] setTintColor:nil];
        [[sortedViews  objectAtIndex:3] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];
    } else {
        [[sortedViews objectAtIndex:3] setTintColor:nil];
        [[sortedViews objectAtIndex:2] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];
    if (allOrMonth == ALL_FLAG_ON) {
        [[sortedViews objectAtIndex:0] setTintColor:nil];
        [[sortedViews objectAtIndex:1] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];
    } else {
        [[sortedViews objectAtIndex:1] setTintColor:nil];
        [[sortedViews objectAtIndex:0] setTintColor:[UIColor colorWithRed: 0/255.0 green:32/255.0 blue:128/255.0 alpha:0.2]];

    // Remove all original segments from the control
    // 一旦Subviewをすべて削除
    for (id view in navigationSegment.subviews) {
        [view removeFromSuperview];
    // Append sorted and colored segments to the control
    // 色を変えたsubviewを付け替える
    for (id view in sortedViews) {
        [navigationSegment addSubview:view];

//subviews sort function
NSInteger static compareViewsByOrigin(id sp1, id sp2, void *context)
    // UISegmentedControl segments use UISegment objects (private API). But we can safely cast them to UIView objects.
    float v1 = ((UIView *)sp1).frame.origin.x;
    float v2 = ((UIView *)sp2).frame.origin.x;
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
        return NSOrderedSame;

Thank you,











How to get people to rate your IOS app

How to display UIAlert like “Rate this application” when application starts.


You should write about UIAlert to didFinishLaunchingWithOptions.

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    CGRect frameForWindow = [[UIScreen mainScreen] bounds];
    window = [[UIWindow alloc] initWithFrame:frameForWindow];

    //write about window

    [window addSubview:navigationController.view];

    //From here about UIAlert and rating popup.
    //manage displaying UIAlert by NSUserDefaults
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger executeTime = [userDefaults integerForKey: @"executeTime"];
    NSInteger alertDispFlag = [userDefaults integerForKey: @"alertDispFlag"];


    //If this application is started 6 times or more and alertDispFlag == 0, then alert is displayed.
    if (executeTime > 5 && alertDispFlag == 0) {
        // アラート表示

        UIAlertView *alertView = [[UIAlertView alloc] init];
        alertView.delegate = self;
        alertView.title=NSLocalizedString(@"pleaseReview", @"Please Rate");
        alertView.message=NSLocalizedString(@"appliciate", @"Please Rate this apps.");
        [alertView addButtonWithTitle:NSLocalizedString(@"Review", @"Rate now.")];
        [alertView addButtonWithTitle:NSLocalizedString(@"Later", @"Remind me later.")];
        [alertView addButtonWithTitle:NSLocalizedString(@"nothanks", @"No, thanks.")];
        [alertView show];
        [alertView release];


    [userDefaults setInteger:executeTime forKey: @"executeTime"];
    [userDefaults synchronize];

    return YES;

//delegate to UIAlret

clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    switch (buttonIndex) {
        case 0:
            //go to rate page
            [userDefaults setInteger: 1 forKey: @"alertDispFlag"];
            [userDefaults synchronize];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:NSLocalizedString(@"reviewURL", @"") ]];

        case 1:

        case 2:
            [userDefaults setInteger: 1 forKey: @"alertDispFlag"];
            [userDefaults synchronize];


review URL is

itms-apps:// digit itunes app id)&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software

Arduino uno + ethernet shield + iPhone + iOSC(OSC Application)

Arduino uno + ethernet shield + iPhone + iOSC(OSC Application)

A LCD didn’t work when I was coding like below.

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

Take care about a LCD connection port.

I referred to UDPSendReceiveString, Arduino examples (Ethernet).


#include          // needed for Arduino versions later than 0018
#include          // UDP library from: 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[]	= { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte ip[]	= { 192 , 168 , 1 , 1 } ;  //

unsigned int localPort = 10000;      // local port to listen on

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back
LiquidCrystal lcd(8, 7, 6, 5, 4, 3, 2);

void setup() {
  // start the Ethernet and UDP:

  lcd.setCursor(0, 0);


void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
    packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Received packet of size ");

    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    char* c = packetBuffer;
    if( strcmp(c, "/osc/button1") == 0 ){
      lcd.print("button 1 pushed");
    if ( strcmp(c, "/osc/button2") == 0 ){
      lcd.print("button 2 pushed");
    Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);

(日本語) iPhone Programming : OpenCV + AVFoundation – First step of AR

隠レター 公開開始

samurai-appsは本日,「隠レター」をリリースしました. ご自分の写真に秘密の画像を埋め込めると言うものです.6月25日までは無料で配布しておりますので,ダウンロードして頂き,ぜひご感想をお寄せください. 詳しくはこちらから!

Calolog FREE 公開開始




Calolog FREE (iTunes)

アイデアの小槌+ Build 20091007.00 公開


アイデアの小槌/アイデアの小槌+ Build 20090811.00 公開


© 2019 samurai-apps. All rights reserved. Powered by WordPress Entries RSS Comments RSS