NSLog InterfaceRotation doesn't work on simulator? - iphone

I want to know why thereĀ“s no console output when testing on the iOS simulator by tracing that code in my UIViewController - it only traces by testing on a device.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(#"willRotateToInterfaceOrientation: ", toInterfaceOrientation);
}
And how do i print out that UIInterfaceOrientation value (type of enum)?
Would be great to get your help on that..Thanks

Where is your format specifier?
UIInterfaceOrientation is a typedef enum, not an object, so you cannot use %# as the format specifier.
Should look like this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
NSLog(#"willRotateToInterfaceOrientation: %d", toInterfaceOrientation);
}
If you really need this kind of "pretty print" functionality, you can run it through a switch, like this:
NSString *orient;
switch(toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeRight:
orient = #"UIInterfaceOrientationLandscapeRight";
break;
case UIInterfaceOrientationLandscapeLeft:
orient = #"UIInterfaceOrientationLandscapeLeft";
break;
case UIInterfaceOrientationPortrait:
orient = #"UIInterfaceOrientationPortrait";
break;
case UIInterfaceOrientationPortraitUpsideDown:
orient = #"UIInterfaceOrientationPortraitUpsideDown";
break;
default:
orient = #"Invalid orientation";
}
NSLog(#"willRotateToInterfaceOrientation: %#", orient);

Related

RemoteControlReceivedWithEvent in iOS 7 issue

I am having a hard time trying to figure out why in iOS 7 the remote controls don't work. In iOS 7, in the lock screen or even in the Control Center, the buttons are unresponsive and the funny thing is that it works fine on iOS 6.
Here is the code I use:
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (player.playbackState == MPMusicPlaybackStatePlaying) {
[player pause];
}
else {
[player play];
}
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
default:
break;
}
}}
This is where I found the information about how to perform this:
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Remote-ControlEvents/Remote-ControlEvents.html
Any ideas why this is happening? It works on iOS 6 but not iOS 7.
Thanks
I ran into this same problem and I ended up removing the case statement UIEventSubtypeRemoteControlTogglePlayPause and added case statements UIEventSubtypeRemoteControlPlay and UIEventSubtypeRemoteControlPause individually. I don't have an good explanation for why this changed.
*UPDATE*
I found that UIEventSubtypeRemoteControlTogglePlayPause is called when the user is using their headset to control the player. Just a FYI.
I think its a better solution:
case UIEventSubtypeRemoteControlTogglePlayPause:
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
if (_paused) {
[self play:self];
} else {
[self pause:self];
}
break;

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.

UIDevice Orientation not returning the correct value

Hey this is the code I am using
switch ([[UIDevice currentDevice] orientation])
{
case UIInterfaceOrientationPortrait:
[self displayActionSheetInPotraitMode];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[self displayActionSheetInLandscapeMode];
break;
default:
break;
}
but when I try it in iOS version 4 or 4.1 on the device
switch ([[UIDevice currentDevice] orientation])
doesn't return the proper value. My application is to run in IOS 4 onwards. How do I rectify this?
Have a look at this
Getting wrong orientation
or else try
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
and then
switch(orientation)
Cheers

UIActionSheet orientation

I am having a problem trying to get the orientation of the device correct. I have to show an action sheet that should come depending on the orientation of the device. Here is the code I am using.
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
switch (orientation)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationUnknown:
case UIDeviceOrientationPortraitUpsideDown:
[self displayActionSheetInPotraitMode];
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
[self displayActionSheetInLandscapeMode];
break;
default:
[self displayActionSheetInPotraitMode];
break;
}
Let's look what the UIDevice Class Reference tells us about the orientation property.
The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.
So, you should call
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]
somewhere earlier in your code.
Also, I'd like to suggest a way to simplify your current code:
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(orientation)) {
[self displayActionSheetInLandscapeMode];
} else {
[self displayActionSheetInPotraitMode];
}
See the answer here: UIActionSheet orientation
You should add a UIView (add it as subview to UIWindow) -> transform it to proper orientation and then add the UIActionSheet to this UIView.

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