UISegmentedControl selected index always 0? - iphone

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.

Related

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;
}

how to change the UI based on the settings of a segmented control

In one UIViewController, I have a UISegmentedControl.
When I select segmented control.selectedindex==0, it will show a textfield.
When I select segmentedcontrol.selectedindex==1, it will show another segmented control instead of textfield.
How can I do that?
Wouldn't you just have 2 segmented controls, but one of them hidden.
When selectedindex==1 on the first one, then unhide the second one.
-(IBAction)yourSegmentControl:(id)sender{
switch ((((UISegmentedControl *)sender).selectedSegmentIndex)) {
case 0:
{
anothersegment.hide = YES;
yourTextfield.hide = NO;
break;
}
case 1:
{
anothersegment.hide = NO;
yourTextfield.hide = YES;
break;
}
default:
break;
}
}
Make sure that in your viewDidLoad you initialize your textfield and secondsegmentcontrol
both to hide = YES;

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)

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.

MKMapView maptype not changing?

I cannot understand why my MKMapView does not want to change to satellite view. This method is called and case 1 is called I have stepped over it but it simply does not change to satellite type it always changes to standard. It only works when it goes back to Map type. Anyone have any ideas?
- (IBAction)mapSatelliteSegmentControlTapped:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex)
{
case 1: //Satellite
self.mapView.mapType = MKMapTypeSatellite;
default: //Map
self.mapView.mapType = MKMapTypeStandard;
}
}
Your MKMapView is always ready to change to the satellite view. But you are forcing it to be in the standard view.
"You missed the break statement in case 1".
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
map.mapType = MKMapTypeStandard;
break;
case 1:
map.mapType = MKMapTypeSatellite;
break;
case 2:
map.mapType = MKMapTypeHybrid;
break;
default:
break;
}
use this code and hookup at xib with "change the value" to segment controll