Re-add a UIButton - iphone

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.

Related

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

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.

Add one button (as subview) to multiple UIViews

I have a UIButton and I want to add it to multiple UIViews. Later on - I want to remove it from one of the superviews. I am trying it the following way but when I pass the removeFromSuperview message to the button, it gets removed from both the views:
[viewOverlay addSubview:myButton];
[viewButtons addSubview:myButton];
Afterwards I want it to be removed from the viewOverlay only.
[myButton removeFromSuperview];
and this causes the button removed from both the views. Any idea how can I achieve this???
Obaid
when I pass the removeFromSuperview message to the button, it gets
removed from both the views
It might look that way, but myButton was removed from viewOverlay when you added it to viewButtons. From the documentation for -addSubview::
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.
The only correct solution here is to create two buttons and add one to each of viewOverlay and viewButtons. The two buttons can look the same, have the same target, and perform the same action, so they'll look like the "same" button from the user's perspective. But a view can only have one superview at a time. I mean that literally: every view has a superview pointer that points to the view that contains it, and that point can obviously only point to one object at a time.
You can't add one view to multiple views at the same time. In this case firstly added view will be removed and added for second. To achieve this you need to create one more instance or copy existed.
From here -
A parent view may contain any number of subviews but each subview has
only one superview, which is responsible for positioning its subviews
appropriately.

Disabling user interaction of all views besides one

I'm trying to simulate a UIAlertView behavior. Basically I want to present a view and disable the user interaction of all other views on screen (besides the presented view). How would I go about doing this?
Your pretend alert view should consist of two views. The first one is the size of the screen and has userInteractionEnabled set to YES. This prevents any touches going through to the views underneath. You then add, as a subview of this view, your actual alert window, with whatever buttons etc. you like.
You can contain both of these in a new UIWindow which you can set the windowLevel on to ensure they are on top of anything else in the screen.
You can also add a very slight backgroundColor to the screen-sized view which will dim everything behind it, if that is appropriate for your interface.
I would recommend adding a view (with user interaction disabled) with black background with an alpha of 0.3 to the applications' main window whenever you show your custom alert. This, in addition to preventing user interaction, also adds that subtle darkening effect to the UI behind your custom alert view.
Call [view setUserInteractionEnabled:NO] on all the views you want user interaction to be disabled. Don't forget to call [view setUserInteractionEnabled:YES]; on them again before you dismiss your custom alert view.

How to change UIActionSheet view?

As I observed that UIActionSheet is always appearing from down side in iPhone,
but in some app, as I saw in AlarmClock, then two button became visible, one from upside and second from downside of iPhone,
What is that exactly, I can't understand, as far as I feel, it looks UIActionSheet,
I want such appearance for one of my app....
Help!
Yes just like Ole said, you can't customize UIActionSheet . But since it is just a simple subview you can create your personal "action sheet" by subviewing and animating. So if you want a button to appear on the top and one on the button, then create two UIViews resize them to the right size for your button, add your action sheet style background or any other background, add your button, connect your IBActions to the buttons and your IBOutlets to the views. Then when you want the buttons to appear animate them onto the screen (using sone CGRect, animation time and finally [UIView commitAnimations])
Probably just one or two custom views with a custom animation. UIActionSheet is not customizable so if you want something else than the default behavior you have to write it yourself. It's not that difficult.

Add UIViewController over another but buttons on parent still clickable

.m file.
- (IBAction)switchViewThinking:(id)sendr {
[self.view addSubview:pick.view];
pick.view.alpha=1.0;
[pick animate];
}
The view pops up fine. Only the buttons on the parent view are still clickable behind the subview that overlays it. Also pick is a UIViewController. The parent view has 3 buttons each open a differnt subview. On the subviews buttons are not clickable through UITables or Scrolltext. Linkage is correct and views remove and activate when needed to. Is there a setting I'm missing that would cause this? Whats the best solution to overcome this.
Thanks
You'll have to either re-create the buttons on the top layer (maybe as transparent clickable views) or make the top layer smaller so that it doesn't obstruct the buttons below it. Instead of one large view on top, you could create many smaller views that look like one view,but that still allow the buttons beneath to be visible and clickable.
In any case, I'd advise thinking whether you really want this behavior. Seems a little like it would not be intuitive for the end user.