iPhone programming-- UIView elements appear to be ignoring Hidden=NO calls - iphone

Using XCode 4.4 and Mountain Lion,
I have a UIImageView, and on that view, I have a UIProgressView and a UIButton. When the app starts, the progress view and button are both hidden, as set in the storyboard. I then try to unhide them, first the progress bar when I'm doing something, and then the button when I'm done. I have called, for both items,
[self.view bringSubviewToFront:saveToCameraRoll];
to try to put them in front of the UIView.
Problem is, when I programmatically try to unhide them, it doesn't work. I can call:
progressBar.hidden = NO;
[self.view bringSubviewToFront:progressBar];
And that does nothing.
So then I tried to set everything as visibile in the storyboard and then programmatically set everything to be invisible once the controller loads. No deal; now my calls to hidden = YES seem to be ignored.
I then removed all actual programming, so that hitting buttons should just cause the button and progress bar to appear, reasoning that maybe the main thread was getting starved and couldn't update.
How can I force elements to pay attention to being hidden?
EDIT: I've now also tried programmatically modifying the alpha from 1 to 0. No change. It's like everything I'm doing is getting ignored. I made these items via the ctrl-drag method into the #interface section of the .m file; maybe I don't have some more delegates or interfaces or whatever hooked up?

As you said that you connected the ivars with your XIB file, it seems like your problem is that you are doing stuff on the main thread which in return blocks the run loop and your UI doesn't get redrawn anymore. When you update UI elements by changing their properties, those changes aren't applied instantaneous but the next time the UI gets redrawn which only happens when you give the main threads runloop a chance to run. However, if you do something like the following code, the changes will never appear:
[button setHidden:YES];
[self doSomethingReallyExpensiveAndTimeConsuming];
[button setHidden:NO];
The result of this code is that the button is set to be hidden, but doesn't get redrawn because the system has no chance to do it and when the system actually has a chance to redraw the UI, the button is already set to be invisible. A fix for this is to either split the work up and schedule it via timers on the main thread, or to use something like GCD to offload the work on a secondary thread (but then you need to make sure that your code is threadsafe!)

Is it possible the outlets are nil because they aren't hooked up in IB? If so, no amount of manipulation will have an effect.
Try NSLog(#"%#", saveToCameraRoll);
Is it null? Fix by reconnecting outlets in IB. If that works, then .hidden = NO will work and you can get rid of any code you added to manipulate the view hierarchy.

Related

Label of UIButton appears intermittently inside a UITableView

I have a UITableView with some data. One of controls is a UIButton, with a label assigned. So, in rare cases, when I push the UITableView screen, the buttons appears without label. After I scroll the UITableView or rotate the device, the label appears.
I put a NSLog entry inside the drawRect method. And It's not called in some cases.
Some advices?
Thanks
i found same problems that some methods rarely not called. make sure delegate is properly set and then check the result
After analyzing my code I saw some characteristic that caused the problem. The process of construction of my screen was occurring in a different thread of the events thread. So, I fixed this and everything is fine now.
Thanks.

iPhone - UITableView background image loads only on every SECOND build

This is driving me utterly insane.
I'm building an iPhone app and in my root view controller I have a UITableView.
In viewDidLoad I've set a background image for this table like so:
[self.view setBackgroundColor:[UIColor blackColor]];// this is so that I can see when the table background image does not show up.
UIImageView* backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"whiteBackground.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 460)];
[self.table setBackgroundView:backgroundView];
[backgroundView release];
My tableCells are transparent (or have background images themselves).
It works every second time I build the app and run it, both on the simulator and on an actual device.
It will show up fine, then when I build again it will show just the black background (but the table calls have their background images/transparency).
Has anyone ever experienced anything like this before? The fact that it is being picked up and displayed every second build makes me think there is something lurking in memory.
When I run it on a device/simulator and then quit the app normally (double tap home, hold the icon and tap closed) and then run it again it works fine. It seems to be something to do with killing the app when I build it each time. Note that I use the same exact code on another viewcontroller and it works every time, but it doesn't work on this - my root view controller.
Do you see the same if you move this code to viewWillAppear instead?
FWIW it would be better to set your backgroundView.frame to self.table.bounds rather than hard coding it to the size of the screen.
I've been banging my head against a wall to fix this and have found out a few things (as mentioned in my reply to Andrew Ebling's response). It's more than just the background image.
I had my project set up like so:
A UIViewController with a XIB.
Drop a tableView in the XIB.
Link up the Delegate and Data Source to the UIViewController.
Create a IBOutlet UITableView named 'table'.
Link that up in IB.
That has always worked just fine for me. But there is something weird going on with my 'table' IBOutlet being recognised 50% of the time. Every second time I build it is not recognised and reloadData is not run whenever I call it. It loads the table first time (meaning the datasource is intact) and cells can be selected (meaning the delegate is fine) but any selectors sent to 'table' are ignored.
This only happens on the RootViewController. Not on any subsequent view controllers in which I use this way of doing things.
So I just changed the class of my RootViewController to UITableViewController, changed the XIB accordingly (replace the uiview object with a uitableview and set the file's owner view property to the UItableview). Basically I just reverted back to what XCode gives you when you specify a new file with a UITableViewController subclass and a XIB for user interface.
That way I use self.tableView to reload instead of my own IBOutlet and it works fine.
I have no idea what caused this though and would like to know if it's a bug or not.

Re-add a UIButton

I'm creating a simple matching game for kids for iPad. All images are drawn on buttons.
When a matching pair is found,
[matchedBtn1 removeFromSuperview];1
[matchedBtn2 removeFromSuperview];
Now, I'm creating a reset function which allows player to reset the once he/she is done.
Question is how do I re-add the UIButtons</code>?
Tried this,
[self addSubview:matchBtn1];
But the program crashes once reset button is clicked and gives this warning for every button I addSubview to
"viewController" may not respond to "-addSubview".
Many thanks in advance for your help. :)
try :
[self.view addSubview:matchBtn1];
I think you should be trying to add them to the view of the controller, not the view controller itself.
[[viewController view] addSubView:matchBtn1]; 
Also make sure you have kept a retained copy of matchBtn1 somewhere in your classes as it will have been released when you removed the subviews.
Instead of adding and removing them, create a set of transparent views over the buttons and when you want to remove them, change the color of those views so that the buttons are hidden. That way if you want to reset the screen, you just have to reset all those views to transparent. You may have to set the cover views to intercept touches too, so the user doesn't click hidden buttons.

iPad VGA output - how to remove the extra UIScreen

I have the need to display a specific view on a separate screen, similar to how Keynotes handles presentation mode.
I've managed this successfully by checking how many screens are available, and then adding the view I want displayed as a subview of the second window.
However, if I dismiss the parent view that handles the creation of the subview, the second screen is never 'disabled' and the view remains on screen. Is there some form of [externalWindow removeSubview] call that I am missing? I release the view I've added to the extra window in the dealloc method of the class that creates it, but am not sure how i should be handling the extra window.
I think the external window keeps a copy of the last output until your app (or another app) outputs something new. Try blanking the external window before disabling output. You can try setting externalWindow.backgroundColor = [UIColor blackColor] or adding a temporary black UIView before disabling output.
Good luck!

In a Tab Bar based app a controller release data of the other ! !

I've made a ViewBased app, in the app delegate i've set a UITabBarCotntroller, in the app i have different view Controller two of them displays text in a UITextView and labels, the other one is my "ShakeController" a UIViewController in which i've set a UIAcelerometerDelegate, in it i create a instance of UIAccelerometer, in the method which manages the shake everything works fine, in this controller i have also set a UIImageView to make a simple animation, in the view Did Load method i set my imageView.animation to an array of UIImage.
My problem is : when the app is launched i use the ViewControllers and everything work fine, but when i tap the ShakeController item in the tab bar and then when i come back to the other controllers the label looks like : label and textView like : Lorem ipsum..... the text of UItextView in IB.
I have noticed thaht if i comment the initialisation of my imageView to the array of image i can navigate the items (from a view controller to another) without the label change and stay what i want them to be.
Notice that the two controllers are in a UINavigationController.
(i use #proprety (nonnatomic, retain) then #synthesize ... then releqse in the dealloc for the labels textview and my uiimageView)
Do not know what to do thanks to all
Quite hard to understand what you're asking here... I re-read it three times and I'm still not sure!
Since you find the problem (the data in the other view being "released") goes away when you comment out your imageView animation initialisation, I would conclude that you are using up a large amount of the iPhone's memory which means that it is automatically unloading any un-seen view controllers. It will do this quite quickly to preserve memory.
You don't say how many images are in your animation, but just 10 medium-large images can be enough to trigger memory issues; if you are wanting to use 20/30+ you have to look for another way.
You can try just loading a few frames into your image first and then swapping them out on a recurring timer. But if it's anything more than very basic animation you'll need to get stuck in to core animation instead: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/WhatisCoreAnimation.html#//apple_ref/doc/uid/TP40004689