MKMapView maptype not changing? - iphone

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

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

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)

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.