Can't touch UITextField on UIScrollView - iphone

I know this has been talked about a lot. I think I've gone thru every question on this site, and still have not been able to get this working.
I'm new to developing but I have a good sense of what's going on with all of my code. I definitely don't have a lot of experience though, this is my first iPhone app.
I'm making a data entry field that is comprised of multiple UITextFields within a UIScrollView. I'll avoid explaining the other details for now, as it seems its a very basic problem. Without a scrollview, the textfields work perfectly. I can touch the textfield and the keyboard or picker view show up appropriately. When I add the textfields to a scrollview, the scrollview works, but then the text fields don't receive my touches.
Here's the key: When 'User Interaction' is ENABLED, the scrollview works but the textfield touches are NOT registered. When 'User Interaction' is DISABLED, the scrollview doesn't work, but the textfield touches ARE registered and the keyboard/picker pops up.
From the other posts I have seen people creating subclasses and overriding the touches in a separate implementation. I've seen people using custom content views (subviews?), I've seen some solutions that are now obsolete since the APIs have changed in newer versions of the SDK, and I am just completely stuck.
I will leave out my code for now, because maybe there is a solution that someone has without requiring my code. If someone needs to see my code, I will put it up. My app is being written in the 3.1.3 SDK.
If anyone has ANY information that could help, it would be so greatly appreciated.

Here is what worked for me in Xcode 4.3.3.
In the storyboard, select your scrollview. Select Attribute Inspector on the right side. Uncheck Delays Content Touches.

It sounds like you're using IB to do a lot of your UI layout. If you take a programmatic approach you could set-up the following view hierarchy which should work.
Your view controller object managing your scroll view and your text fields should have a UIScrollView object and a UIView object (in addition to any UITextField objects you need). In the loadView method of the view controller class, allocate and initalize the UIView object and add your text fields to it as subviews. At the end of the method, allocate and initalize the UIScrollView object then add the UIView to the UIScrollView as a subview.
As an example, if your UIScrollView object were called scrollView and your UIView object called mainView the following lines at the end of the view controller's loadView method would properly set up the scroll view with the main view with the text fields on it:
scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] bounds] ];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = mainView.frame.size;
[scrollView addSubview: mainView];
self.view = scrollView;

You may need to enable / disable user interaction for both the scroll view and text fields as necessary.
If possible you might best using different touch mechanisms for each process (scrolling and textfield input).
Maybe you can check the time differential between touch start and end such that you can detect the difference between a drag (scroll) and a tap (text input).
Having said that if you propagate the touches up from the textfields to the scrollview you should be fine. One thing I use is:
[myObject addTarget:self action:#selector(itemTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
which passes the object touched so you can identify it and act accordingly.

Related

Touch detection problems with custom View inside UIView NOT UIScrollView

I was googling for a while and I found similar problems but when the custom View is inside a ScrollView, but that is not my case.
I have a custom view that consists of a UILabel behind a UITextField, so I can animate that label later.
The problem is that when I add a View in my ViewController and in the Identity Inspector I set the Class as my custom class, when I use the application the UITextField within my custom view does not receive the touches well and it takes time to gain focus and therefore to open the keyboard. The strange thing is that if I move that same arrangement of views to my main ViewController in Storyboard everything works fine. Why doesn't it do it when I place it using the described method?
I plans to reuse this custom view a lot, so putting logic and views in each ViewController is not an option.
Thanks in advance
Well, the problem was in the constraints of the container UIView. That means, the UIView in my main ViewController. The Height of the UIView was a little bit smaller than the space required for my custom view, so although my custom view seemed to draw correctly, it was not receiving the gestures correctly. The solution was simply increase the height to the correct value occupied by my custom View. Thanks a lot!

iOS /Obj-C UIScrollView and PageControl not working

I am attempting to implement a UIScrollView with a UIPageControl following the guide on this website:
http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
In opposite to the guide, I am implementing this in a storyboard project, and using XCode4.5.2. In addition to the code in the guide, I have added at the beginning of the #interface, and inside the viewDidLoad method in the implementation, I have added scrollView.delegate = self.
With only the code and IB-objects described in the guide added to my app, it doesn't allow me to scroll at all when I test it on my iPhone. The only scrolling i can do is by swiping my finger over the UIPageControl, which makes the "white dot" in the PageControl move one spot towards the direction I was swiping, and with no visible changes in the ScrollView. In the guide we add different colours to the backgrounds of the views in the scrollView -- none of these colours show up at all.
I have tried debugging by putting NSLogs in my code to see what methods are reached by the app. Adding an NSLog (#"View did load"); right after [super viewDidLoad]; in the viewDidLoad method atop the main view controller doesn't output anything to the terminal. Does this mean my app doesn't load up properly?
Thanks in advance!
Do you have multiple pages added to the scrollView? Scrolling is default off if you only have one page worth of content. Ensure that the loadScrollView method is called and also add this:
scrollView.pagingEnabled=YES;
If you are interested I created this class for paging with the scrollview
https://github.com/andrealufino/ALScrollViewPaging

How to manage objects plans in a view iphone sdk

I'd like to know how to manage objects plans.
I explain my issue :
I have a UITextField and a Label, and I'm trying to place the label behind the TextField in the view, but anything to do, the Label stay visible and in front of the TextField, How to change the plan position of object into a view ?
Every visible objects in a UIView is a subclass of UIView and all UIViews have the method brigSubviewtoFront because so:
[self.view bringSubviewToFront:textField];

Touch on UIButton in a UITableViewCell cannot scroll the table

I need to have a UIButton inside a UITableViewCell, everything works fine except when I touch inside the button and move my finger, the table does not scroll.
I tried to subclass UIButton and in touchesMoved: method send the same message to self.nextResponder, or call touchesCancelled and hope it will pass the touch event to next responder, they both do not work.
Is there any good way to solve this problem? Currently I am adding a UIView on top of the UITableViewCell, and detecting touch events manually, passing result to the button or the rest of the cell respectively, but this is a little bit dirty.
What you can try is setting the delaysContentTouches property of the UITableView to YES.
Additionally you can set the canCancelContentTouches to YES.
If the value of this property is NO, the scroll view does not scroll
regardless of finger movement once the content view starts tracking.
Source: UIScrollView Class Reference
Try:
_yourTableView.delaysContentTouches = YES;
_yourTableView.canCancelContentTouches = YES;
The best way is to make a custom cell and do your work neatly.
Check the Apple docs

Swipe to change views

Is the following easy to code?
I have a tableview and when the user selects a cell a detail view is loaded.
I want to allow the user to navigate throughout the items/detail-view representing the items in the tableview by left and right swipes, working in the same way as e.g. the home-screen of the iphone (e.g. while swiping, one page is moving off the screen and the next appears).
I have seen this implemented in the MobileRSS-APP, so it is possible, but I just have a hard-time figuring out the way to do it properly.
Thanks a million in advance! I am still trying to find my feet with cocoa-touch...
This is not a secret, Apple provides the sample code called the PageControl that demonstrates exactly what you want to achieve:
This application primarily demonstrates use of UIScrollView's paging functionality to use horizontal scrolling as a mechanism for navigating between different pages of content.
I implemented it in many of my apps.
The iphone home screen (springboard) works with a scroll view set to paging enabled.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
Then, lets say there's two pages of apps to swipe through - you set the content size of the scroll view to twice the width of your view:
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width*2, self.view.frame.size.height)];
And thats how that is done. :) I don't know if its the same for the MobileRSS-APP.
Things to bear in mind:
This isn't the default way a table view works. You might get your app rejected by apple. A table view is meant to work by clicking.
You could probably replicate the functionality without using a scroll view using touches methods and the navigation controller. A good tutorial describing how to detect swipes on your view is here: http://www.dosomethinghere.com/2009/07/23/simple-swipe-detection-in-the-iphone-sdk/
In the last method in that tutorial where there is the NSLog("swipe right") you could replace that with [self.navigationController popViewControllerAnimated:YES] which would replicate pressing the "back" button in the navigation bar.
I don't know what you could do for swipe left though..... if you wanted to do this on the table view you'll have to do some complicated maths determining how far the table view has been scrolled. Or you could test for touch events on the cells themselves, but thats something you wil have to look into. :)