Home Tutorial Iphone Examples iPhone UISwitch Action

 
 

Share on Google+Share on Google+
iPhone UISwitch Action
Posted on: September 30, 2010 at 12:00 AM
Advertisement
UISwitch Class: Provides a method to control on and off state in your application. For example if you wanted to receive a alert when your application receive any updates you can set it to on, if your application got the switch feature.

iPhone UISwitch Action

UISwitch Class: Provides a method to control on and off state in your application. For example if you wanted to receive a alert when your application receive any updates you can set it to on, if your application got the switch feature.

In the example we will discuss about the same feature "UISwitch" control and will connect it to a action that will send a action message.

a simple example: UISwitch Action

-(IBAction)switchingbtn:(id)sender {

if(switchControll.on){

[switchControll setOn:NO animated:YES];

showMessage.text = @"Switch is OFF!";

}else{
[switchControll setOn:YES animated:YES];

showMessage.text = @"Switch is ON!";

}

This is a simple action that will be further connected to UISwitch in .sib file. The action will recognize the status of switch control and returns a corresponding message.

You can create you UISwitch either programmatically or simply drag and drop a UISwitch control in the .Xib (nib file). We used the .sib as it's easy and fast. If you are using .sib then declare the UISwitch in header file as given below…

#import <UIKit/UIKit.h>

@interface SwitchActionViewController : UIViewController {

UISwitch* switchControll;
UILabel* showMessage;

}

@property(nonatomic, retain)IBOutlet UISwitch* switchControll;
@property(nonatomic, retain)IBOutlet UILabel* showMessage;
-(IBAction)switchingbtn:(id)sender;

@end

and synthesize the switchControll.. see the code below..
#import "SwitchActionViewController.h"

@implementation SwitchActionViewController
@synthesize switchControll;
@synthesize showMessage;

- (void)dealloc {
[super dealloc];
[switchControll release];
[showMessage release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
showMessage.text = @"Switch is ON!";
}

-(IBAction)switchingbtn:(id)sender {

if(switchControll.on){

[switchControll setOn:NO animated:YES];

showMessage.text = @"Switch is OFF!";

}else{
[switchControll setOn:YES animated:YES];

showMessage.text = @"Switch is ON!";
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

@end

The output is:

Download Code

Advertisement

Related Tags for iPhone UISwitch Action:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 30, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com