Cocoa Touch UITextView setText: method useless - iphone

I have a UITextView which I create in Interface Builder for an iOS app. I checked (and double checked, and triple checked) that it is properly connected to the outlet in my code. I create a property and synthesize it, and when I run NSLog(#"%#", myTextView);, it returns the UITextView's properties, not null. However, when I try calling [myTextView setText:#"My Text."];, it doesn't change anything on the view. I have found many people with similar problems like this on Stackoverflow and other places on the internet, however none of the solutions there helped me. I am using Xcode 4.0.2 on Snow Leopard. I am attempting to do this in the -viewDidLoad method. What could be causing this issue?
If you need any more information about my code setup, post a comment and I will update this post.

Are you sure the whole text view is visible on screen, and that the font color is not the same as your background?

It is very likely that the view that is displayed on the screen is not the same view as myTextView points to. Check [myTextView superview] and make sure it's onscreen. Then look and see if you've accidentally covered it with something. Change other aspects like the backgroundColor. Also, check the frame and make sure it's not CGRectZero.

Related

Changes made to UI objects are not updating in iPhone simulator

I'm new to objective-c and iPhone, but have done lots of java coding in past.
Tearing hair out about v. basic things which I'm usually eventually able to find answer to, but not this one!
I'm wanting to make a label's background colour change, code below. No matter what I do it doesn't change when I run it on the iPhone simulator. Is there some setting there, I feel like I'm asking a very stupid question but really can't find an answer. Is this a problem with the setup of the development environment??
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"setting label to black color");
self.colourLabel.backgroundColor = [UIColor blackColor];
}
Why don't you make an IBOutlet of type UILabel, hook it up to the label in the interface builder and do:
yourLabel.backgroundColor = [UIColor whateverColor];
Did you set colorLabel as a property? Because you wouldn't need to... unless you have two separate classes that you want to communicate with each other.
Remember that in Objective-C sending messages to nil objects is fine, so the problem may be that self.coulourLabel is still nil at that time.
How and where do you instantiate it? Did you set the IBOutlet?
Just thought I'd leave an answer emphasising the suggestion from #Dima in the comments on the question - ie, Project > Clean and then iOS Simulator > Reset Content And Settings.
I just solved a similar problem which was occurring with a UIButton created in code, and so couldn't have been an IBOutlet issue. Changes I made to the code (even deleting the property altogether) weren't showing up when I ran in the simulator, even though on my device it worked fine. I had assumed that doing a clean would be enough but it wasn't until I did the Reset Content And Settings that it started behaving as I expected.
Hope someone finds this useful...

Difference between view.hidden = x and [view setHidden:x]

I'm debugging an iphone app and I'm seeing something I don't understand fully.
Based on user's selection, a UIView is being shown or hidden. Current code shows or hides the view with [view setHidden:NO] and [view setHidden:YES]. This doesn't work: visually it's as if these statements are simply ignored. However when I changed these to view.hidden = NO and view.hidden = YES respectively, everything is working as expected.
I was thinking that the two syntaxes are equivalent, but apparently not. For all other attributes (text, font, etc.), both work identically, so what's so special about hidden?
EDIT: Here's the copy/paste of some of my code. I'm working in XCode 4.3 with iPhone simulator 5.0
Here's one example from my project.
IBOutlet UIView *panel; //Connected in interface builder
===========
- (void)makePanelVisible:(BOOL)visible
{
[panel setHidden:!visible]; //this does not work
panel.hidden = !visible; //this does work correctly.
}
Sets whether the view is hidden.
- (void)setHidden:(BOOL)flag
Returns whether the receiver is marked as hidden.
- (BOOL)isHidden
hidden=YES; is identical to setHidden:YES; The difference is, you are turning hidden to YES right away Whereas sethidden view disappears from its window and does not receive input events because it is set to be hidden. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual.
Thinking back about this, I remember running into the same issue almost 3 years ago, when iPhone 3 (not even 3G) was all the rage. I'm not sure why this happens, but it does - so I just deal with it by setting the property using the "dot" notation. I guess, this is one of those "don't fix it if it ain't broken" things (ok, it is sort of broken, but there's an easy way around it, so I'm using it).
hidden is a property of UIView. When you wrote [panel setHidden:YES], you try to call the method setHidden that should set the property hidden. It doesn't work because the method doesn't exist in the UIView : https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816
I always set the property by writing "view.hidden = x".
I hope it will help you

UISegmentedControl Problem setting hidden

Hi I regulary created using IB one UISegmentedControl that I called showAllSwitch.
If I try to do [showAllSwitch setHidden:YES]; nothing happens!
Why? How can I do to hide it?
Where are you calling setHidden? if it is being called before it is added to the view, then there could be problems. Try calling it in viewDidAppear and see it that works. If it doesn't either the segmentedControl is not connected correctly in IB, or there is a rather big problem that we are missing.
Hiding an object is relatively straight forward. So if there is a problem then its something simple. You need to just go through some basic diagnostic steps:
Verify that your segmented control is actually connected to the correct outlet in IB. Really. Go look. Even if you are sure. Go look again.
Verify that the line where you are hiding it is being called. Add an NSLog just after and see if it shows up when it should.
Make sure that the hide command is not getting sent too soon. If its being sent in ViewDidLoad try setting it up in ViewDidAppear.

How can I send characters to cursor position of UISearchBar?

I am trying to add a couple characters that are inconveniently located in the normal keyboard, and place them in a toolbar so that the user can use them just like normal keys.
Does anyone have a useable way to do this?
I found an article explaining how to do this by simulating a "Paste" operation, (remove pasteboard contents, replace with my character, paste into field, return original pasteboard contents) but my trouble is that I'm trying to do this with a UISearchBar, which seems to have no paste selector.
Update
I found a lead:
UIKIT_CLASS_AVAILABLE(2_0) #interface UISearchBar : UIView {
#private
UITextField *_searchField;
Since it is documented that there's a UITextField in a search bar, if I were to root through the searchbar's subviews and locate said text field, (assuming with 99% certainty that the text field has a delegate) would it make sense that I could "steal" the text field and make my class the delegate, then forward the messages to the original delegate once I'm done with them?
This is definitely tricky. UISearchBar doesn't give you inputAccessoryView and nor do you get selectedRange.
You can paste in a UISearchBar. If you want to get your tricky characters to the pasteboard, you could get a button to execute something such as:
[[UIPasteboard generalPasteboard] setString:#"[*]"];
and then get the user to use paste in the UISearchBar. Pretty awkward for the user though.
Rooting through the subviews to find the UITextField might work. If you do this, you'd need to grab the existing delegate and make yourself the delegate. Then your delegate would need to transmit messages on. The process is described in this stackoverflow question and answer. Potential challenges here: (a) the Apple implementation could change between iOS updates and even, though unlikely, change the delegate during the lifetime of the UISearchBar; (b) Apple might see this as using a private API and reject the app. (I don't have any hard evidence of (b), but it's something to consider.)
One approach might be to use the bookmark button. The UISearchBar delegate can detect this. You could use that to insert your special characters or offer up a menu of special character insertions. Of course, you won't know where the cursor is. But, depending on your use case, appending the special characters at the end might be OK. Perhaps this doesn't get you anything over a button on your interface that just appends something.
[[self searchBar] setText: [NSString stringWithFormat:#"%#[*]", [[self searchBar] text]]].)
Implementing your own search bar might be the best way to go as already suggested #hyperbole. I've done this successfully by adding a custom UITextField (with my own magnifying glass in the leftView slot etc.) and adding it as the titleView of my navigationBar. But, if I understand your question aright, that still won't be enough, as UITextField doesn't provide selectedRange and its delegate doesn't provide an equivalent of textViewDidChangeSelection:. You might have a go with a UITextView that is fixed to one line (with scrolling clamped down if required - it often seems to be).
Can't you simply set the text of the UISearchBar? Of course, the tricky part is to determine the cursor position. For that, you can register a UITapGestureRecognizer on the UISearchBar, determine the tap co-ordinates & calculate the cursor position using - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode or its variants.
You may also have to register a UIPanGestureRecognizer, as the user can change the cursor position by tapping, dragging & then releasing the finger.
HTH,
Akshay

iOS - status bar randomly turns solid black

Developing an iPhone app.
I've got a really strange problem where, every once in a while, the status bar at the top of my app screen will turn solid black. Not like the black version of the status bar, but like a solid black rectangle with NO text/icons. It's very rare, but usually seems to occur after returning to the app via multi-tasking or from a locked device (the app has been running in the background). I've seen it occur on both 3GS and iPhone4. Here's a screenshot:
I can never reproduce it when trying, it just seems to eventually happen at some point (sometimes it will go for days without happening).
Once it does occur, the app seems to continue functioning fine, even with the status bar gone, except for when I do one specific action in the app which will cause everything to freeze up all the sudden (the app doesn't crash, but everything on screen is frozen and non-interactive). Without explaining the design in detail, the specific action that causes it to freeze up (after the bug appears) is performing a simple upload in the background to a SQL database. Resetting the app is the only way to fix the problem once the black status bar appears.
Anyone else ever experienced this? I can't find a single thread anywhere explaining similar behavior, and it's driving me nuts.
It happened once in my app when I called a drawing method in my custom subclass of UIView instance right before I added it as a subview to parent view.
The solution was apparently easy: add it as a subview first before sending/calling any custom drawing methods.
Examples:
CustomView *aView = [[CustomView alloc] init];
[aView drawSomething];
[self.view addSubview:aView]; //wrong approach
[aView release];
Should be:
CustomView *aView = [[CustomView alloc] init];
[self.view addSubview:aView];
[aView release];
[aView drawSomething];
The screenshot is missing, but what you describe sounds as though you've incorrectly implemented the use of Apple's built-in view controllers.
Both UINavigationController and UITabBarController will automagically shift all the content inside them down by 20-pixels, if they detect there is "supposed" to be a statusbar on screen at the moment.
My guess is that you have some code that is removing the statusbar, but which is kicking-in after the Apple code has already detected it and shifted everything down to accomodate.
The "fix" is to re-read the docs on Apple's classes very carefully and use them as Apple dictates (usually, people use them in ways that seem sensible - e.g. embedding them inside other views - but which Apple has expressly declared are unsupported. Sadly those classes from Apple are very fragile)
Are you holding a reference to a QLPreviewController instance? I was able to solve this problem in my app by creating a new autoreleased QLPreviewController whenever I need to display a file modally, as opposed to reusing the same instance over and over again.
I had a similar problem, which I described in this question here
If you have any, try removing any CGRect frame created by reference to:
[UIScreen mainScreen].applicationFrame]
and instead create the frame using a more manual definition. If that works, you can decide how to proceed from that point.