how to change label text on click on segmentControl in iphone - iphone

I am using segmentedControl in iphone application i want that when
with YES and NO
I have also one lable i want that when YES is selected lable should display YES other wise
but its not changing with my code
-(IBAction)segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
self.segmentLabel.text =#"YES";
break;
case 1:
self.segmentLabel.text =#"NO";
break;
default:
break;
}
}

Very simple use
setTitle:forSegmentAtIndex:
So the code should looks like
NSString *str;
switch (self.segmentedControl.selectedSegmentIndex) {
case 0: {
str = #"YES";
break;
}
case 1: {
str = #"NO";
break;
}
default:
break;
[self.segmentedControll setTitle:str forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
or simpler version
if (self.segmentedControl.selectedSegmentIndex == 0) {
[self.segmentedControll setTitle:#"YES" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}
else if (self.segmentedControl.selectedSegmentIndex == 1) {
[self.segmentedControll setTitle:#"NO" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}

Look at the image and as per that do proper wiring (select Value changed option).
-(IBAction)valueChanged:(id)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0) {
[self.segmentedControll setTitle:#"YES" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}
else if (self.segmentedControl.selectedSegmentIndex == 1) {
[self.segmentedControll setTitle:#"NO" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}
}

Related

How to implement Kobold2D KKInput gestureSwipeDirection to if else statements?

I'm finding it hard to learn from documentation on how I can use the Kobold2D KKInput gestureSwipeDirection to detect swipes left/right/up/down and have them carry out if else statements. Can anyone help by providing me with a sample code. Thanks
KKInput* input = [KKInput sharedInput];
KKSwipeGestureDirection dir = input.gestureSwipeDirection;
switch (dir)
{
case KKSwipeGestureDirectionRight:
// direction-specific code here
break;
case KKSwipeGestureDirectionLeft:
// direction-specific code here
break;
case KKSwipeGestureDirectionUp:
// direction-specific code here
break;
case KKSwipeGestureDirectionDown:
// direction-specific code here
break;
}
I think you're making a mistake, you're placing the code in a single method, but you should use two hands, one to determine the KKInput, and one to check the status, plus you forgot gestureSwipeEnabled
try to do this:
-(id) init {
if ((self=[super init])) {
input = [KKInput sharedInput];
input.gestureSwipeEnabled = YES;
[self schedule:#selector(theTime:)];
}
return self;
}
-(void) theTime:(ccTime)time {
if (input.gestureSwipeRecognizedThisFrame) {
KKSwipeGestureDirection dir = input.gestureSwipeDirection;
switch (dir)
{
case KKSwipeGestureDirectionRight:
// direction-specific code here
break;
case KKSwipeGestureDirectionLeft:
// direction-specific code here
break;
case KKSwipeGestureDirectionUp:
break;
case KKSwipeGestureDirectionDown:
// direction-specific code here
break;
default:
break;
}
}
}

hide previous button only when the page number is zero and next button only when page number is 20

I have two buttons with objects *tmpltLeft and *tmpltRight. When i launch my app, i want the previous button(tmpltLeft) initially to be hidden and when i press the next button(tmpltRight) i want the previous button(tmpltLeft) to be displayed and when the next button(tmpltRight) reaches page 20(last page) I want the next button(tmpltRight) to be hidden.
I have in .h file
IBOutlet UIButton *tmpltLeft, *tmpltRight;
In .m file
-(IBAction)templateNavigationBtnTapped:(UIButton*)sender {
UIButton *button=sender;
switch (button.tag) {
case 1:
NSLog(#"prev btn tapped");
if (pageNo>1) {
pageNo--;
}
break;
case 2:
NSLog(#"next btn tapped");
if (pageNo<18) {
pageNo++;
}
break;
default:
break;
}
First Set [tmpltLeft setHidden:YES]; in -viewDidLoad
-(IBAction)templateNavigationBtnTapped:(UIButton*)sender {
UIButton *button=sender;
switch (button.tag) {
case 1:
NSLog(#"prev btn tapped");
if (pageNo>1) {
[tmpltRight setHidden:NO];
pageNo--;
if (pageNo == 1)
{
[tmpltLeft setHidden:YES];
}
}
break;
case 2:
NSLog(#"next btn tapped");
[tmpltLeft setHidden:NO];
if (pageNo<18) {
pageNo++;
if (pageNo == 18)
{
[tmpltRight setHidden:YES];
}
}
break;
default:
break;
}
Not Implemented , but write here to get some Idea to you...
You can check the page number and then hide the button. As per your code:
-(IBAction)templateNavigationBtnTapped:(UIButton*)sender {
UIButton *button=sender;
switch (button.tag) {
case 1:
NSLog(#"prev btn tapped");
tmpltRight.hidden = NO; // for the next button in the last-1 page to be displayed if the page number is less than max number of pages.
if (pageNo>1) {
pageNo--;
}
else if (pageNo == 0)
tmpltLeft.hidden = YES;
break;
case 2:
NSLog(#"next btn tapped");
tmpltLeft.hidden = NO; //for the previous button to be displayed when the page number is greater than 0.
if (pageNo<18) {
pageNo++;
}
else if (pageNo == 19)
tmpltRight.hidden = YES;
break;
default:
break;
}
The code is not tested, but that is the general idea.
- (void)viewDidLoad{
[super viewDidLoad];
BtnCount = 0;
previosButton.enabled = FALSE;
}
-(IBAction)previosButtonPressed:(id)sender{
BtnCount= BtnCount-1;
if(BtnCount==0){
previosButton.enabled=FALSE;
}
if(BtnCount<19){
nextButton.enabled= TRUE;
}
}
-(IBAction)nextButtonPressed:(id)sender{
previosButton.enabled = TRUE;
BtnCount = BtnCount+1;
if(BtnCount==19)
{
nextButton.enabled = FALSE;
}
}
switch(button.tag) { case 1:
NSLog(#"prev btn tapped");
if (pageNo>1 || (tmpltLeft.hidden=TRUE)) {
tmpltRight.hidden=FALSE;
pageNo--;
if(pageNo==1)
tmpltLeft.hidden=TRUE;
}
break;
case 2:
NSLog(#"next btn tapped");
if (pageNo<18 || (tmpltRight.hidden=TRUE)) {
tmpltLeft.hidden=FALSE;
pageNo++;
if(pageNo==18)
tmpltRight.hidden=TRUE;
}
break;
default:
break;
}
Initially tmpltLeft button is hidden in
(void)viewDidLoad {
// Template Navigation Button
tmpltLeft.hidden=TRUE;
}

Whats wrong with this line?

My app crashes when I press the "own" button. I don't know what's wrong with my code. I'm new to this, so if I totally screwed up, don't be to hard please :). EDIT: This is what Xcode says:
#1 0x000144fd in -[UIApplication sendAction:to:from:forEvent:] ()
The app crashes on the line:
if (thing.hidden == NO) {
This is the code that belongs to "own":
- (IBAction)own {
if (thing.hidden == NO) {
int rNumber = rand() % 4;
switch (rNumber) {
case 0:
result.text = #"A";
break;
case 1:
result.text = #"B";
break;
case 2:
result.text = #"C";
break;
case 3:
result.text = #"D";
break;
default:
break;
}
}
if (thing.hidden == YES) {
int rNumber = rand() % 3;
There may be several rason
IBAction is actually just a hint that tells interface builder where to find the methods in your objects so you can link controls to methods.
In iOS, actions can take zero, one or 2 parameters.
If one parameter, that parameter is the object sending the message:
-(IBAction) someAction: (id) sender;
If two parameters, it takes the form:
- (IBAction) someAction:(id) sender forEvent: (UIEvent*) event;
you can use -(void)own and connect to your button in nib file
Nothing seems wrong in your code, assuming that the objects thing and result are retained properly.
- (IBAction)own {
if (thing.hidden == NO) {
int rNumber = rand() % 4;
NSString *myText = #""; //
switch (rNumber) {
case 0:
myText = #"A";
break;
case 1:
myText = #"B";
break;
case 2:
myText = #"C";
break;
case 3:
myText = #"D";
break;
default:
break;
}
result.text = myText;
}
if (thing.hidden == YES) {
int rNumber = rand() % 3;
I just met this question. It's because of the memory management, I send the button related action to a released viewController. Hope this help.(BTW,I use the arc mode)

UISegmentedControl selected index always 0?

I have a UISegmentedControl that has 6 segments which I created in Interface Builder. I am calling the following method on value changed:
-(IBAction)segmentedChartButtonChanged:(id)sender
{
switch (self.segmentedChartButton.selectedSegmentIndex) {
case 0:
NSLog(#"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
case 1:
NSLog(#"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
default:
break;
}
}
Whenever I change the segments, the selectedSegmentIndex is always 0. Why is this?
It is very likely that the IBOutlet isn't connected. Check that. You can also use the sender that is passed to the method.
UISegmentedControl * segmentedControl = (UISegmentedControl *)sender;
switch (segmentedControl.selectedSegmentIndex) {
....
hi try like this it will work
-(IBAction)segmentedChartButtonChanged:(id)sender
{
UISegmentedControl *segment=(UISegmentedControl*)sender;
switch (segment.selectedSegmentIndex) {
case 0:
NSLog(#"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
case 1:
NSLog(#"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
break;
default:
break;
}
}
Take an outlet for UISegmentedControl and handle with that like this,
if (self.paymentSegmentButton.selectedSegmentIndex ==0)
{
NSLog(#"first view ");
}
else
{
NSLog(#"payment initiation");
addVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"secondView"];
[self.navigationController pushViewController:addVC animated:YES];
}
I hope it helps someone.

On shake... iOS

So I have an IBAction yesNo that I want to be ran on a shake event. Not all too sure why this is not working. Have followed all the documentation.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[self yesNo];
}
}
And then the IBAction itself:
- (IBAction)yesNo
{
int rNumber = rand() % 26;
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
if ([[def objectForKey:#"activeVersion"] isEqualToString:#"0"])
{
switch (rNumber) {
case 0:
result.text = #"Never";
break;
case 1:
result.text = #"If you're lucky...";
break;
case 3:
result.text = #"Think twice";
break;
...
default:
break;
}
}
else if ([[def objectForKey:#"activeVersion"] isEqualToString:#"1"])
{
switch (rNumber) {
case 0:
result.text = #"Never1";
break;
case 1:
result.text = #"If you're lucky...1";
break;
...
case 25:
result.text = #"Very doubtful2";
break;
default:
break;
}
}
else if ([[def objectForKey:#"activeVersion"] isEqualToString:#"3"])
{
switch (rNumber) {
case 0:
result.text = #"Never3";
break;
...
case 25:
result.text = #"Very doubtful3";
break;
default:
break;
}
}
}
Basically what I have is a fortune ball type thing and when the iPhone is shaken I need that IBAction run.
Have you made that view the first responder? I.e. [yourView becomeFirstResponder]; (probably from some viewDidAppear: method).
You might want to check if it actually is the first responder when you shake your device.
When I was switching from defining everything graphically in IB to programmatically inline in Xcode, I forgot to make the view the first responder at all. Here's the code that ultimately fixed it:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self becomeFirstResponder];
NSLog(#"self is first responder %i",[self isFirstResponder]);
}
First thing I would question is whether this should be defined as an IBAction. If you only call it from code then you might want to consider using (void) instead (Simply a style choice).
Secondly, are you sure that the method is actually being called? Throw an NSLog in there to make sure.
Thirdly, are you sure that [def objectForKey:#"activeVersion"] returns a string? Is the value returned what you would expect? Throw an NSLog in there to make sure.
My guess is that one of the NSLogs will give you the answer to your question as the rest of your code seems fine.