Wednesday, 26 June 2013

How to create SegmentedControlButton to change color of textfield-text in Xcode

 Here, we will teach you how to make a segmented control button with 3 segments, and how it can be used for changing color of text in an UITextfield.




in .h:

        @interface ViewController : UIViewController{
            IBOutlet UISegmentedControl *colorChooser;
            IBOutlet UITextView *sampleText;
        }
        @property(nonatomic, retain) UISegmentedControl *colorChooser;
        @property(nonatomic, retain) UITextView *sampleText;

        -(IBAction)colorChanged;
        @end

in .m:

        @implementation ViewController

        @synthesize colorChooser,sampleText;

        -(IBAction)colorChanged{
            if (colorChooser.selectedSegmentIndex==0)sampleText.textColor= [UIColor blackColor];
            if (colorChooser.selectedSegmentIndex==1)sampleText.textColor= [UIColor blueColor];
            if (colorChooser.selectedSegmentIndex==2)sampleText.textColor= [UIColor redColor];
   
        }
in xib:
 
  • Drag and drop a segmented control view, and make it a 3 segmented one.
  • Drag and drop a text view
  • From "Connections inspector-window", connect:
                          Segmented Control : Value Changed            ---->colorChanged
                          Segmented Control : New referencing outlet ---->colorChooser
                          TextView          : New referencing outlet       ---->sampleText 



Now Build and Run the application. You can see the text field and 3 segmented button. Click on each segment and see the change in color of the textfield text.

 

No comments:

Post a Comment