UISwitch control not working properly in Xcode 5 - uidatepicker

Recently i updated my Xcode 4.6.3 to Xcode5 DP2.Everything was working fine in Xcode 4.6.3.I mean inside a Viewcontroller there is a UISwitch and UIDatePicker and two buttons "set" and "clear". When I select a particular time from UIDatePicker and change the UISwitch state to "on" and by clicking the "set" button an alarm is set for the selected time. And if I change the UISwitch state to "off" UIDatePicker will show the current time otherwise the selected time.This was working fine in Xcode 4.6.3. But after updating to Xcode5 DP2,everytime i toggle the UISwitch state,UIDatePicker is returning back to the current time,I mean even if i select a time other than the current time in UIDatePicker and toggle UISwitch to "on" state UIDatePicker returns back to current time.I dont know what is going on here.Is there any problem with Xcode5 DP2..Please help me..
This is my code:
-(IBAction) switchValueChanged
{
NSString *value = #"OFF";
if (switch1.on)
{
value = #"ON";
[[NSUserDefaults standardUserDefaults] setObject:value forKey:#"stateOfSwitch"];
[switch1 setOn:YES animated:YES];
}
else
{
[datePicker setDate:[NSDate date]];
[[NSUserDefaults standardUserDefaults] setObject:value forKey:#"stateOfSwitch"];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
[[NSUserDefaults standardUserDefaults]synchronize];
}

I had the same problem, I spend too much time trying to make it work, but apparently it's an issue with the xcode 5 simulator.
Just run it on a device and you should see that the nsuserdefaults will be working fine.
Hope this serves.

Related

Adding a UIDatePicker as an inputView to a textField in iOS 8

I have an UIDatePicker in my storyboard view connected to an IBOutlet in the header file.
In the implementation file I set some properties to the picker and then assign it to my textFields:
[self.txtEndDate setInputView:self.picker];
This was working fine in iOS 7, but with iOS 8 it's giving me the following error:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7c2d8800> should have parent view controller:<InserimentoDurata: 0x7aec2b10> but requested parent is:<UIInputWindowController: 0x7b92b400>'
Any idea on how to fix this?
After receiving an email from Apple's Developer Technical Support, it seems that to add a UIDatePicker (or any custom keyboard for what I've understood) to a viewController, you don't have to add it to its view anymore, but you add it to its title bar and then connect it to the IBOutlet.
It's working for me, even if it doesn't work in the iPhone 5 simulator (all the others are ok) and I was going nuts.
I hope this could be of help for other people with the same problem.
The solution is to build your UIPickerView in code (remove it from the Storyboard), assign it to the textfield's inputView, and retrieve it from there anytime you need it (instead of keeping a reference to it). Basically, this means:
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 300, 320, 168)];
[picker setDataSource: self];
[picker setDelegate: self];
picker.showsSelectionIndicator = YES;
self.textField.inputView = picker;
If you later need it, use:
UIPickerView* pickerView = (UIPickerView*) self.datePartySizeTextField.inputView;
[pickerView selectRow:1 inComponent:0 animated:NO];
UIDatePicker should not be child of any super view
Problem:
You have to ensure that the view you will assign to inputView or inputAccessoryView don't belong to any parent view. Maybe when you create these views from xib inside a ViewController, by default they are subviews of a superview.
Solution Tips:
Using method removeFromSuperview for the view you will assign to inputView or inputAccessoryView
see detail in this link
Error when adding input view to textfield iOS 8

UITextField no longer reloads keyboardType after reloadInputViews call

In iOS 7, I could change the keyboard type while it is the firstResponder (on the fly):
if (textField.text.length > 2) {
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
else
{
textField.keyboardType = UIKeyboardTypeDefault;
}
[textField reloadInputViews];
// (Omitting some efficiency stuff to keep example to bare bones)
This no longer works under Xcode 6/iOS 8. The documentations mostly reflect changes regarding custom keyboard.
Using resign/become first responder is (still) working:
[textField resignFirstResponder];
// Make keyboard change
[textField becomeFirstResponder];
But it just feels like an overkill. It's tearing and rebuilding a wall, just to change a picture on it.
There is a related post here:
UITextView does not seem to implement reloadInputViews
But it seems that the solution (in a comment) is "apparently declaring it as a UITextView instead of a UIResponder affects how it behaves during runtime. ... and it works now"
In my case it is a UITextField, and I tried to cast to UITextView just in case. No go.
I'll mention again that it is working well under iOS7 / Xcode5.
I don't really know if this is a 'beta' issue with Xcode 6, or a design change in iOS 8.
I found the same issue. It is better to check whether the textField is already the firstResponder or not.
[textField reloadInputViews]; // does not work on iOS8 !
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
[textField becomeFirstResponder];
}
Not a clean way though, but it works.
I found that this works when the textfield is first responder:
[self.textField reloadInputViews];
[self.textField setText:#" "];
[self.textField setText:#""];

UISwitch doesn't work on Google Maps SDK for iOS?

I'm trying to create a UISwitch laid on mapView_ of Google Maps for my iOS app, but it seems not to work.
In details, I first followed instruction from google, created mapView_, then made it my viewcontroller's view:
self.view = mapView_;
Then, I created an UISwitch programmatically and added it as a subview:
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 360, 0, 0)];
[mySwitch setBackgroundColor:[UIColor clearColor]];
[mySwitch addTarget:self
action:#selector(changeSwitch:)
forControlEvents:UIControlEventTouchDown];
[mapView_ addSubview:mySwitch];
But when I touched the switch both in simulator and device, it didn't change its state from ON->OFF or OFF->ON. I even tried different UIControlEvent, such as UIControlEventValueChanged but it didn't work. To make sure that the code should work, I tried on a normal view of a normal test viewcontroller (that means, not using google maps), it worked fine!
Does anyone have any comment about this issue?
Thanks heaps!
You can work around this issue by adding both the UISwitch and the GMSMapView to a single UIView parent, instead of adding the UISwitch as a child of a GMSMapView. Yes, this means you need to position both the GMSMapView and the UISwitch.

UIPopover button repeats action

I am creating the application for iPad with the UIPopover. I have a main view from which I am calling two popovers displaying different information. I was following the guidelines in How to Dismiss a Storyboard Popover thread and another threads, everything works fine except one thing. In my popover I have a button which triggers an action on the parent view. From time to time the action is triggered more than once even if the button was clicked just once and also popover was opened just once. My first assumption was that the popover was caching some data from several calls, but the problem seems to appear just randomly.
My configuration is: Mac OSx Snow Leopard with Xcode 4.2, iOS 5.0.
Tested in Simulator, iPad 5.1 and iPad 6.0 all the same results.
I have the main view View 1 and the popover view View 2
In my view 2 I have a method ProceedButtonClicked which sends the notification to the View 1
- (IBAction) ProceedButtonClicked{
[[NSNotificationCenter defaultCenter] postNotificationName:#"proceedButtonClicked" object:self];
}
The method is binded to the button in the popover view.
In view1 (parent view):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ButtonClicked:) name:#"proceedButtonClicked" object:nil];
}
- (void) ButtonClicked:(NSNotification *) notification {
NSLog(#"I'm here ...");
//dismiss popover
if (paramPopover)
[paramPopover dismissPopoverAnimated:YES];
}
I am pretty new to the iPad development, so maybe I am missing in my code something obvious, but searching until now resulted to nothing.
Any help would be appreciated.
Thank you.
When using notifications, you risk multiple instances of a class answering the same notification, so if you have 2 controllers alive for some reason (bad memory management?), then when pressing the button 2 controllers will answer the call and you will have a duplicate action.
Buttons can have a specific callback assigned to them, and it's very simple to set it up by code:
If your button is a UIButton, you can set your target action like this:
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
If your button is a UIBarButtonItem, you set the target when you create it
[[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:self action:#selector(buttonAction:)];
Edit:
NSLog(#"I'm here ...");
That's creepy ...

iOS6 ScrollBarShouldScrollToTop not firing/ ScrollView Delegate issue

I am adding a dummy ScrollView to my app to detect a user click on the status bar, to performa an event in my program.. I am creating it in the ViewDidLoad:
//Dummy Scroll is for the tap on status bar to work
UIScrollView *dummyScrollView = [[UIScrollView alloc] init];
dummyScrollView.delegate = self;
[[self view ] addSubview:dummyScrollView];
[[self view] sendSubviewToBack:dummyScrollView];
I then implement :
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
NSLog(#"scrollViewShouldScrollToTop");
.
.
}
Under all previous versions of IOS this has worked beautifully and flawlessly, yet under iOS 6 the scrollViewShouldScrollToTop never gets called. Is this a bug?? The API says this should still be available as part of the delegate in iOS6, yet under iOS6 on both device and simulator it never executes... Anyone have any idea what is going on?
Still no other TableView or ScrollView, but there is a MAPVIEW?? But the MapView doesn't have a shouldScrollToTop that I can find to set to NO.. so I am still beyond confused why this stopped working under iOS 6...
Is there any chance that the UIScrollView you're creating isn't somehow the only UIScrollView in your view hierarchy? It looks like in iOS6, if you have more than a single UIScrollView in your view hierarchy, only one should have scrollsToTop = YES. This is the one that'll have its scrollViewShouldScrollToTop method called.
My problem was similar in that I had a very basic UITableView that would no longer autoscroll to the top when the status bar was tapped. I finally remembered that one of the cells in my tableView uses a UIWebView, and that the cell's webView.scrollView was (correctly, now in iOS6) hijacking the call to scrollViewShouldScrollToTop that, before iOS6, was being made on my tableView.
After setting the tableViewCell's "scrollsToTop = NO", the status bar autoscroll once again worked as it did before. Here's more-or-less how the code looks:
myCustomCellWithAWebView.webView.scrollView.scrollsToTop = NO;
Hope this helps!
On iOS 6, only tap the part above scrollview of status bar can fire scrollsToTop event.
And, that scrollView can't be hidden or 0 alpha.
But it can be covered. or clear background color.
So on iOS 6, you need
dummyScrollView.frame = self.view.bounds;
dummyScrollView.backgroundColor = [UIColor clearColor];

Resources