How to reload my UIViewController on click of some buttons? - iphone

I'm developing an iPhone app, i have a UIViewController class where some coverflow animations are there and according to design i have some 5 buttons on top of my view each button have an IBAction method on clicking a button i need to display different set of coverflow with different data and the count of coverflow also changes from 1 button action to another. My problem is how can i refresh my page(reload) on click of these buttons, as i know calling explicitly viewDidLoad is a very bad idea, so how can i accomplish this? Any help is appreciated in advance thank you.

The easiest way to get a view to re-draw itself is to call [UIView setNeedsDisplay] or by using [UIView setNeedsDisplayInRect]. This method tells the view that it needs to re-draw itself and it's subviews. However you shouldn't always have to call this. There are other methods that can cause views to redraw themselves. For instance if you add or remove a view to the view hierarchy using [UIView addSubview:] or [UIView removeFromSuperview].
These methods cause the system to call your view's drawInRect: method (assuming you overrode it with custom drawing code). If you did not override this method for custom drawing then it should cause your subviews to receive the same message so that they can get a chance to redraw.
Additional information here. Specifically read the section labeled "The View Drawing Cycle".
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

Why do you want to reload the page?, if it is because you are changing your view and you want it to be redrawn, you can call:
setNeedsDisplay();
But that is often not needed, as whenever a views bounds change that is called automatically for you.

If there's too much other stuff rather than refreshing/reloading the part of your interest in your viewDidLoad method, you can extract the refreshing part into a private method, say methodA. Every time you click your button, you call that methodA. However I think methodA still need call setNeedsDisplay or re-draw the the part of your interest. Hope it helps.

If your point is to refresh "UIViewController", then:
[self viewDidLoad];
Source: How to refresh UIViewController programmatically?

To Refresh your UIViewController, When Clicking the button >>
Add the below code into your UIButton method,
-(IBAction)button:(id)sender{
[self viewDidLoad];
[self viewWillAppear:YES];
}
Now Build and Run Your Code.

Use function viewWillAppear().This function is called everytime the view is called.

Related

UIViewController multiple UIView

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

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];

uiautomation - Tapping custom view doesn't work

I am trying to automate some tests of my ipad application.
I have a scrollview which contains a custom view.
The custom view overwrites the drawRect and has a TapRecognizer.
The custom view is created into the code and I have set this properties
myView.userInteractionEnabled = YES;
[myView setIsAccessibilityElement:YES];
[myView setAccessibilityLabel:#"myView"];
The custom view is added to the scrollview with
[myScrollView addSubview:myView];
Everything works smooth both on device and on simulator: tapping on the view, the tap recognizer callback is called and the custom view can draw something at the tap point.
I would automate the view test and then I need to simulate user's taps on myView.
In the uiautomation script I have something like this:
myView = circuitScrollView.elements()[0];
myView.logElement();
myView.tapWithOptions({x:56, y:576});
to simulate a user tap at position x=56 and y=576.
Nothing happens, it seems that myView doesn't receive any tap (just in case, I play a sound into the TapRecognizer but it has never been sounded).
I have tryed this too:
myView.tap();
no success.
Any idea ?
Thank you in advance.
Fab.
This may be of some use to you. I wrote a test that taps certain x/y coordinates. Rather than touching the scroll view try tapping the window as demonstrated below.
To do so I wrote:
var window = UIATarget.localTarget();
window.tap({x:x_co , y:y_co});
x_co and y_co were my coordinates.
Hope this helps.
Maybe myView = circuitScrollView.elements()["myView"] might help instead of myView = circuitScrollView.elements()[0]; ?
Maybe your custom view is not the first element in the element tree of your circuitScrollView...

Force a UITextView to redisplay after changing contentInset?

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?

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.