how can I remove a UITextField from a View? I've assigned a tag to the UITextfield and I search it from the subviews in the main view. But when I found it, it do a strange thing: doesn't execute the after instruction, or exactly it execute the instruction, but do nothing. Instead if I hidden it, all works fine.
Why won't you use removeFromSuperview selector?
You should by able to completely remove the UITextField by calling - (void)removeFromSuperview on the main thread.
You can remove your views by using removeFromSuperview
and should be like ---> [myTextView removeFromSuperview]
I think there might be other views over UITextView.Since
[myTextView removeFromSuperview];
should work fine.You can remove any of the view which is in SuperView.
So, please make sure that UITextView is still there ....or from somewhere else still textView is allocated & added to subivew.
Either you can set the Different BackGround Colors for TextView so that you can easily trace whether what is removed from SuperView.
Related
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.
This is just to confirm my solution:
I have two UIView that I would like to swap between controlled by the UIViewController
In IB I have created 3 UIViews:
TopView
A_View
B_View
Then in ViewDidLoad I add [
[self addSubView] A_View];
Then in some IBAction method I do this
[A_View removeFromSuperview];
[self.view addSubview:B_View];
This is indeed swapping the two views, with TopView just sitting there. But my first thought was just to call [[self view] removeFromSuperView]]; and then add B_view.
This gives me an empty screen?
So is my solution the right way?
Thanks in advance
Regards
Christian
Looks fine to me. It's a pretty normal swapping style, just remove one and add the other.
You should always watch out with modifying the UIViewController's view itself. Your solution is fine.
That way is fine and perfectly straightforward.
If you're on iOS 4.0 or later, you can also use the transitionFromView:toView:duration:options:completion: method in UIView, which removes and adds for you, plus provides a transition animation.
The reason [[self view] removeFromSuperview] left you with a blank window is because you're removing your controller's base view object (including it's subviews, e.g. Top_View, etc) from it's superview (likely the window object). Your solution works when you remove A_View and add B_View, as both are being added or removed as subviews to your controller's view, i.e. self.view.
I want to redraw a view.
When I touch a button, my view have to redraw itself. (have to call viewWillAppear)
But view's setNeedDisplay method doesn't redraw itself immediately.
Because I have to redraw it immediately, the method is not suitable to me.
I tried to remove all of viewController's view's subviews and to change viewController's
view to other view.
But these didn't work. (don't redraw)
I need your help acutely.
Thank you for your reading.
I think there are some misunderstandings here. Let's set things straight:
viewWillAppear: has nothing to do with the drawing of the view.
It's true that setNeedsDisplay doesn't redraw the view instantly, but we're talking milliseconds. So that's not even relevant.
Since you want viewWillAppear: to be called I'm assuming that what you call redrawing the view really should be referred to as relayouting the view. I assume that what you do in viewWillAppear: is setting the frames of you're views and possibly add/remove and/or show/hide some views.
I suggest that you move that code out of viewWillAppear: and into it's own method that will be called from viewWillAppear: and when you tap the button.
Since you don't provide your viewWillAppear: code this is all based on assumptions, so if my assumptions are wrong, please let me know and please do provide your "redraw" code.
Without more details it's hard to know exactly what you need but I might suggest you look at UIView::setNeedsDisplay. This will in turn cause your views drawRect: to be called where you can update the drawing.
[yourViewName setNeedsDisplay];
I change the contentInset. How can I force it to update the display to reflect the new value?
I found that it did update the contentInset immediately, but it didn't look like it because it adjusts the location that the UITextView is scrolled to such that the display of the UITextView doesn't change.
By scrolling back to the top-left corner, it has the effect I think you're looking for of updating the display with the new content inset:
[textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
I have a similar issue if not the same issue.
I was able to get setNeedsDisplay to work, but you have to use it and place it on the right view and call it in the right order or it won't work.
I have a view that I want to reuse, all I change is a UITextView and an image on the view depending on where it is opened from. I have the label accessible via Outlet on the controller. I could tell that my assignment was working as the changed value would show up the 2nd time I went back to the modified view. I could not get any of the previously suggested ways to work until I hooked up setNeedsDisplay on the controllers view (NOT the UITextView, as I tried that and it did not take) and I had to do it just before I make the text change as it did not work if I made the call after the setText. Anyway, maybe setting it up in this way and in that order may work for you.
Try using [textView setNeedsDisplay].
The only way I could think of is to re-init it with the new properties. Sounds like a waste, though.
You could also try [textView layoutIfNeeded] or [textView layoutSubviews].
I take it back, the documentation of this method suggests that:
The default implementation of this
method does nothing.
and
You should not call this method
directly.
Does [textView setNeedsLayout] do it?
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.