Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2012, 06:35 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default UITextView touch crashes app

My app has multiple UITextViews and touching a text view causes the background to change color. But after a few touches the app crashes.

My app crashes with EXC_BAD_ACCESS when the user touches a UITextView (it doesn't crash on the first touch, but after some random number of touches) No help from the stack trace or zombies.

I have replicated the problem with one UITextView and minimal code shown below. The code given causes a EXC_BAD_ACCESS when the UITextView is touched a few times..

I would greatly appreciate any advice....

Code:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextViewDelegate>

{

}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

@property (strong, nonatomic) IBOutlet UITextView *text_view_1;

@end

Code:
#import "ViewController.h"
@implementation ViewController
@synthesize text_view_1;

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
    [text_view_1 setDelegate:self];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    //Code to change textview background colour will go here
    return NO;
}
DrSpike is offline   Reply With Quote
Old 01-24-2012, 10:53 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Colorado, USA
Posts: 7
ShawnArney is on a distinguished road
Default

Is this UITextView hooked up/connected in the xib? Otherwise you are referencing it without the property being setup for you. Setup the delegate of the UITextView in the xib. Rather than on the load method. It should be in the init method or you can set it in the xib... to avoid threading (re-entrant) issues with these references to self and UITextView.

Cheers,

Shawn Arney
iOS Professional Developer & Trainer

Learn iPhone Apps at: LearnApps.Org - Keeping it Simple: iPhone Training and Tutorials
ShawnArney is offline   Reply With Quote
Old 01-25-2012, 02:18 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by ShawnArney View Post
Is this UITextView hooked up/connected in the xib? Otherwise you are referencing it without the property being setup for you. Setup the delegate of the UITextView in the xib. Rather than on the load method. It should be in the init method or you can set it in the xib... to avoid threading (re-entrant) issues with these references to self and UITextView.
Thanks for the reply.
I tried setting up the delegate in the xib (and deleting the setDelegate call). The end result is exactly the same behavior.

I also tried putting it in init:
Code:
- (id) init
{
    [text_view_1 setDelegate:self];
    return self;
}
but when i touch the UITextView the keyboard pops up so it isn't setting the delegate properly.
DrSpike is offline   Reply With Quote
Old 01-25-2012, 03:13 AM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

You shouldn't mess with any UIView's property in the init method of your controller because the view isn't created yet.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-25-2012, 03:13 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
You shouldn't mess with any UIView's property in the init method of your controller because the view isn't created yet.
Ya I thought as much but just wanted to try ShawnArney's suggestions.

I'm still stuck on this problem. Am I missing something obvious?
DrSpike is offline   Reply With Quote
Old 01-25-2012, 03:27 PM   #6 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

How do you handle the touches on the UITextViews? Your code seems ok so the problem must be somewhere else.

Also you don't need to define
- (BOOL)textViewShouldBeginEditingUITextView *)textView;
in your .h file because it's a delegates method.
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 01-25-2012, 03:38 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2011
Location: 127.0.0.1
Posts: 51
Pyroclastic is on a distinguished road
Default

Are you running this in Simulator on 4.3? If so, go to settings and turn off the auto correct and anything that is on. See if the problem persists. I had a similar issue and doing this resolved it for me.

It's just a but with the Simulator I believe.

EDIT: I had this issue with the UITextField ... but it's worth a shot
Pyroclastic is offline   Reply With Quote
Old 01-25-2012, 04:37 PM   #8 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by apatsufas View Post
How do you handle the touches on the UITextViews? Your code seems ok so the problem must be somewhere else.

Also you don't need to define
- (BOOL)textViewShouldBeginEditingUITextView *)textView;
in your .h file because it's a delegates method.
Thanks I deleted that from the .h file but the problem persists.

The code I posted above is all it takes to cause the problem. There is no other touch handling and no other additional code in my project apart from the xcode generated single view stuff. I was having the problem in a much larger project but managed to localize the problem to the few lines of code I posted.
DrSpike is offline   Reply With Quote
Old 01-25-2012, 04:47 PM   #9 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by Pyroclastic View Post
Are you running this in Simulator on 4.3? If so, go to settings and turn off the auto correct and anything that is on. See if the problem persists. I had a similar issue and doing this resolved it for me.

It's just a but with the Simulator I believe.

EDIT: I had this issue with the UITextField ... but it's worth a shot
Hi, I am running on simulator 5.0. But I also have the problem on my iphone running the latest ios.

I had Capitalization and Correction off but still I have the problem. Thanks for the suggestion.
DrSpike is offline   Reply With Quote
Old 01-25-2012, 05:14 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

UPDATE: The problem seems to be specific to the simulator and devices running ios 5....

My app was selling fine on ios 4 but the bug reports started to come in after ios 5 was released. Also, I can reproduce the problem with the above code on ios 5 but not ios 4. I'm pretty stumped now.

I wonder is there a work around... Any other way to cause a UITextView to change background color when touched but not show the keyboard?
DrSpike is offline   Reply With Quote
Old 01-25-2012, 05:55 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

What is the reason that you're using a text view instead of a label?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-25-2012, 06:31 PM   #12 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
What is the reason that you're using a text view instead of a label?
Hi, I posted a simplified version of the code just to focus on the problem but my app does have some situations where the user should edit the text...
DrSpike is offline   Reply With Quote
Old 01-25-2012, 06:59 PM   #13 (permalink)
[[Brain alloc]init];
 
fhsjaagshs's Avatar
 
Join Date: Aug 2011
Location: New Jersey
Age: 15
Posts: 92
fhsjaagshs is on a distinguished road
Default

First of all, I make it a point never to retain an IBOutlet (by indicating strong). AFAIK, IBOutlets, even when not declared as properties are by default retained.

Before you try something a little involved, changing the @property to a regular declaration.

if that fails:
If you need to manage the touches for your UITextField, create a method in the View Controller that handles the touch, make sure it's a - method not a + method. Then make a subclass of UITextField that handles the touches. Use this:

In InterfaceBuilder, remember to set the UITextField to the class of the custom text field. Also make changes to the declaration of the instance variable.
Code:
// In the main view controller header (MyMainViewController.h)
- (void)customTextFieldWasTouched:(NSSet *)touches withEvent:(UIEvent *)event;

// In the main view Controller (MyMainViewController.m)
- (void)customTextFieldWasTouched:(NSSet *)touches withEvent:(UIEvent *)event {
       // Handle the event
}

// In the UITextField subclass
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       MyMainViewController *mmvc = [[MyMainViewController alloc]init];
       [mmvc customTextFieldWasTouched:touches withEvent:event];
       [mmvc release];
}
fhsjaagshs is offline   Reply With Quote
Old 01-25-2012, 07:01 PM   #14 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

There is no way that code is crashing all by itself without there being some other issue. There is something else going on that you haven't shown.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-25-2012, 08:59 PM   #15 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by fhsjaagshs View Post
First of all, I make it a point never to retain an IBOutlet (by indicating strong). AFAIK, IBOutlets, even when not declared as properties are by default retained.
You might get away with it using only an instance variable, but:

1) If you DO add a property, then the behavior of that property will be whatever you define it to be. So if you define it as an assign property, then you've overridden whatever behavior might have been applied to the instance variable alone. The XIB loader looks for properties first via KVC, and only uses instance variables if they aren't found.

2) As a general guideline, you should always use properties.

3) Also as a general guideline, anytime you want to make sure that an object remains alive on your terms, you should retain it. This means using a retain property. That way if Apple changes their minds about how XIBs load, it won't affect you. This is less of an issue with ARC, but still sound advice.

Combined, you are making it a point to do the wrong thing. Chances are that the only reason you haven't been bitten by this (or didn't realize you had) is that you haven't removed any IBOutlets from their superview intending to use them again later. Or ARC has been saving your bacon.

Quote:
Originally Posted by fhsjaagshs View Post
Before you try something a little involved, changing the @property to a regular declaration.
It is regular for ARC.

Quote:
Originally Posted by fhsjaagshs View Post
Code:
// In the UITextField subclass
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       MyMainViewController *mmvc = [[MyMainViewController alloc]init];
       [mmvc customTextFieldWasTouched:touches withEvent:event];
       [mmvc release];
}
So you are creating a brand new view controller, sending it a message, and then getting rid of it. How does that help pass a message along to the view controller that is already alive, already managing the view that is already on the screen?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-26-2012, 12:29 AM   #16 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
There is no way that code is crashing all by itself without there being some other issue. There is something else going on that you haven't shown.
I start with a clean project, single view and only add the code I posted.

I made a video of the project and crash here:
Screen Recording 6.mov - YouTube
DrSpike is offline   Reply With Quote
Old 01-26-2012, 01:02 PM   #17 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Ok, this I have to see. Please post a link to your sample project.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-26-2012, 01:17 PM   #18 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Ok, this I have to see. Please post a link to your sample project.
Thanks for your help. Project attached.
Attached Files
File Type: zip Test_Debug_MCQ.zip (30.8 KB, 6 views)
DrSpike is offline   Reply With Quote
Old 01-26-2012, 02:24 PM   #19 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Wow. I have absolutely no idea. Unfortunately you are using both ARC and Storyboards, so I can't help very much and the things I would want to try I can't do. I tried different names, I even added a text view to the iPad Storyboard, no change. Crash after the 2nd or 3rd tap.

If I return YES, then it seems to work just fine. So it would seem to be an issue with returning NO, but I have no idea why that would matter.

I'm afraid I'm stumped. Sorry.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-26-2012, 03:03 PM   #20 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Wow. I have absolutely no idea. Unfortunately you are using both ARC and Storyboards, so I can't help very much and the things I would want to try I can't do. I tried different names, I even added a text view to the iPad Storyboard, no change. Crash after the 2nd or 3rd tap.

If I return YES, then it seems to work just fine. So it would seem to be an issue with returning NO, but I have no idea why that would matter.

I'm afraid I'm stumped. Sorry.
very strange indeed. I attached a project with no storyboard and no ARC to this, but the behavior is the same. It is only the ios 5 sim and devices that crash but ios 4 works fine.
Now that I know your machine is giving the same result, maybe I should post a bug report on the apple site...
Thanks
Attached Files
File Type: zip new_test_bug.zip (27.6 KB, 6 views)
DrSpike is offline   Reply With Quote
Old 01-26-2012, 03:17 PM   #21 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Yep, still an issue.

I even - uh, don't tell anybody this - checked the retain count, and if anything the number was higher than I expected. My vote is for Apple bug. It sure does act like an over-release. I wouldn't even know what to change to work around it, other than to allow editing. Maybe swap back and forth between a label for reading and a text view for writing, although that would be challenging if you need to scroll.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-05-2012, 04:08 PM   #22 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 2
rikgig is on a distinguished road
Default This is probably an Apple bug

Hi all
I confirm that I also have this issue only on the simulator, my iPad running iOS 5.0.1 seems to take this correctly.

What I am doing, I have subclassed the UITextView so that I can respond to the method - (BOOL)canBecomeFirstResponder so that this memo will be in read only mode but allow scrolling when the text is viewed only and not edited.
First click in the simulator runs ok, but will crash every time with the second click in it.

This may have something to do with an internal gesture recognizer of this control.

I can see this as the last code label before it crashes:
_ZN2TI8Favonius10BeamSearch20choose_hit_test_nodeE RKN3WTF6RefPtrINS0_10SearchNodeEEERKNS3_INS0_11Key AreaNodeEEES7_S7_+14>

I would also vote for an Apple bug.




Quote:
Originally Posted by BrianSlick View Post
Yep, still an issue.

I even - uh, don't tell anybody this - checked the retain count, and if anything the number was higher than I expected. My vote is for Apple bug. It sure does act like an over-release. I wouldn't even know what to change to work around it, other than to allow editing. Maybe swap back and forth between a label for reading and a text view for writing, although that would be challenging if you need to scroll.
rikgig is offline   Reply With Quote
Old 02-07-2012, 09:25 PM   #23 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 2
rikgig is on a distinguished road
Default Solved... sort of...

Hi again guys.

I've been playing around a bit to go around this issue and found, if I can say this, something.
Instead of just preventing the control from going in Edit mode based on the delegate method or by doing what I just did, that is subclassing the UITextView and overriding canBecomeFirstResponder, just use the editable property.
By setting this guy to NO, you still get the scrolling feature to work and also, TADA, no crashes.

I still believe this is some sort of bug in the API itself. I've trace the machine code and with the labels we see in there, we can see that we are going through the GestureRecognizer code. And still stranger, I have another UITextView, shown this time in a Popover, and this one never crashes...

Anyway, I'll be looking more closely at properties next time, editable is the key!

Hope it helps!

Eric.

Quote:
Originally Posted by BrianSlick View Post
Yep, still an issue.

I even - uh, don't tell anybody this - checked the retain count, and if anything the number was higher than I expected. My vote is for Apple bug. It sure does act like an over-release. I wouldn't even know what to change to work around it, other than to allow editing. Maybe swap back and forth between a label for reading and a text view for writing, although that would be challenging if you need to scroll.
rikgig is offline   Reply With Quote
Old 02-08-2012, 10:39 AM   #24 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 11
DrSpike is on a distinguished road
Default

Thanks for the input.
I need my UITextView to change background color when touched so it has to stay editable.
I ended up switching to UILabels for the time being.
regards
DrSpike is offline   Reply With Quote
Old 04-05-2012, 01:41 PM   #25 (permalink)
Registered Member
 
Join Date: Apr 2012
Posts: 1
balexander is on a distinguished road
Default

DrSpike, did you ever resolve this with UITextView? Anyone know if an apple bug was ever identified for this? I have a similar occurrence where I get an EXC_BAD_ACCESS because either a UITextTapRecognizer or UIVariableDelayLoupeGesture object is released one time too many down in UIKit or Foundation, which I found by enabling zombies and profiling the app.
balexander is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 400
12 members and 388 guests
7twenty7, Atatator, condor304, FrankWeller, glenn_sayers, iphonedevshani, MAMN84, mraalex, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:52 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0