In my app I have a custom UIView subclass (let's call it MyView) which contains three buttons and two labels. I add this view to a view controller which also has a table view (I add the instance of MyView at the bottom).
Because of the business logic rules, the labels and one button out of three are hidden in the beginning. So I do this in viewDidLoad:
self.myView.label1.hidden = YES;
self.myView.label2.hidden = YES;
self.myView.button1.hidden = YES;
which works fine. So these three are hidden and the remaining two buttons are visible.
Now this view controller is also a delegate for another class. At some point in time an event occurs in this other class which calls a notification method in my view controller.
In this notification method I have now to show the hidden controls. So I obviously tried the following:
self.myView.label1.hidden = NO;
self.myView.label2.hidden = NO;
self.myView.button1.hidden = NO;
but it doesn't work, they don't appear.
Any idea what I'm doing wrong? Do I need to somehow "repaint" self.myView after this so that the controls become visible? What am I missing here?
Many thanks in advance!
Edit
I have added some NSLogs after setting them visible and the logs show something like this:
label1.hidden = 0
label2.hidden = 0
button1.hidden = 0
So as per the logs, they should be visible.
Ok, so I solved the problem. I moved the code that sets the visibility of the controls in another method and I call this method like this:
[self performSelectorOnMainThread:#selector(updateControls) withObject:nil waitUntilDone:NO];
So what do you know, the notification method was called in another thread (I didn't know this, the library I am using is actually not mine and there's nothing in the docs about this fact).
Anyway, it's nice that it works now.
Thanks all!
Have you confirmed that your notification method is getting called? You shouldn't need to specifically refresh the view, but if you are sure that your method is called, then you can also try adding [self.myView setNeedsDisplay]; into your notification method.
Related
In iOS, is there a possibility of stopping a view from loading in the viewWillAppear ?
Something like [self close]; ?
What I am trying to do is:
create a view with an object partially loaded
display the view
in the viewWillAppear, I finish the object loading and fill the view fields
if the object loading fails, I do not want to show the view
I know that it is not really the good way to do that,
but it's existing code that I do not want to change too much.
I'm not sure if I misunderstood your question, but if you just try to have a view inside a viewController not to be shown at the beginning and to appear later you can easily viewName.hide = YES; it in viewWillAppear and then viewName.hide = NO; when you want it to be shown.
When I am moving the buttons on the screen from a function, [self makeButtons], nothing happends unless I push a viewcontroller, then pop back. Is there a reload data function for ViewController, as it is for UITableViews? I am using NavigationController, and I am adding subviews to a UISrollView and moving other buttons on the screen. The method is called after fetching data with ASIFORMHTTPRequest.
EDIT: I am sorry I didn't specify more.
I have a method that is sending a [request startAsynchronous] (ASIFORMHTTPrequest).
I have a NSMutableArray containing all my buttons. When the request is done, a method called doneGettingRequest, which looks like this.
- (void) doneGettingRequest(ASIFORMHTTPRequest *) request {
[self removeButtons];
}
which is calling this method;
- (void) removeButtons {
NSLog(#"Removing buttons!");
for (UIButton *button in gameButtons) {
[button removeFromSuperview];
}
Everything works when I go to another view, then back again. The problem is it won't refresh if THAT view is being shown when the method is called (which will happend almost always). The gameButton is a NSMutableArray containing buttons that are currently being showed. When I am done removing them, I want to add some other buttons. The problem is, the buttons is not removed when the removeButtons is called. The message "Removing buttons!" however, is shown, so the method is being called.
Any suggestions?
Thanks
You can put your logic in your viewWillAppear.
This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view.
You can override this method to perform custom tasks associated with displaying the view.
If you override this method, you must call super at some point in your implementation.
Have you tried
[view setNeedsDisplay];
I am not able remove view. I am adding view using addsubview method but when I use RemoveFromSuperView.
// Here I am adding view from login button
medicalViewObject=[[MedicalInfoViewController alloc] initWithNibName:#"MedicalInfoViewController" bundle:nil];
[self.view addSubview:medicalViewObject.view];
//here I am writing RemoveFromSuperView in another ViewController
-(void)BackToMainView
{
[self.view removeFromSuperview];
}
It is not working because you are sending removeFromSuperview to the wrong view object. This is what you need:
[medicalViewObject.view removeFromSuperview];
EDIT:
Your question suggests that you have a view (because you did not include the name of this view in your question, I will simply call it MainView) and then you add a subview called medicalViewObject to your MainView. You also have a method call "BackToMainView" which you want to perform the removeFromSuperview function. Your question suggests that some user action in your medicalViewObject (such as a button press) is supposed to call the "BackToMainView" method.
If this is all correct, then my answer above is correct. But based upon your comment, it sounds like you will also need to implement a delegate protocol in your medicalViewObject, and then have your "MainView" adopt the protocol.
In your declaration of the delegate in your medicalViewObject, you need to have a method call like this:
-(void)backButtonWasPressed;
and in the implementation of your MainView it should look something like this:
-(void)backButtonWasPressed
{
[medicalViewObject.view removeFromSuperview];
}
So now, whatever user action you are using in your medicalViewObject to go back to the main view (whether a button or some other object) it needs to call the following:
[delegate backButtonWasPressed];
Depending on your situation it may look a little different, but this a fairly common way to accomplish what you are trying to do. I hope this helps.
when you call [self.view removeFromSuperview]; you are telling whatever view controller you are in to remove its own view. You should be calling that line from within that exact same MedicalInfoViewController or telling medicalViewObject from the outside to remove its view like [medicalViewObject.view removeFromSuperview];
My application is loading a first view (used to login into a Web service). When the login is successful, it performs a CATransition (basic kCATransitionFromRight) to show a second view and hides the first view. I've set the delegate of the transition to self so I can use -(void)animationDidStop:(CATransition *)theAnimation finished:(BOOL)flag.
When that method is called (right after the transition is over) I want to release the first view since I won't need it anymore. However, when I call [firstView release] (in animationDidStop:) the retain count doesn't seem to change. I used [loginView retainCount] to check this and since I know it's not always reliable I was wondering: am I doing this right?
Thank you.
taken from the book "Cocoa Touch for iPhone OS 3" is a similar approach.
They set up an animation remove the old subview, add the new one and then commit the animation.
Jilouc in his comment is right, forget to check "retaincount"...
if you want to be sure that your object view firstView just add a
NSLog(#"i'm removing myFirstView");
in its
-(void)dealloc{
}
method...
if you get that NSLog in debugger console window then be sure you had it removed/released in the right way...
btw... the right way could be something like this:
in animationDidStop:
if (firstView!=nil){
[firstView.view removeFromSuperview];
[firstView release];
firstView=nil;
}
So in my app delegate I add a call add the myViewController.view to the main window:
1. [window addSubview:myViewController.view];
In myViewController I do the following code in the viewDidAppear method:
2. [self presentModalViewController: yourViewController animated: YES];
In my yourViewController class I do the following to try and go back to the main window
3. [self dismissModalViewControllerAnimated:YES];
My main windows view appears with buttons in all, but the buttons won't react to any click or anything. It's like there is something over them that I can't see.
Also, the main windows button works before this process but doesn't after the process.
Any help would be appreciated.
If the dismiss method call is in the modal view controller (not the parent that presents it), then you actually want to call [self.parentController dismissModalViewControllerAnimated:YES];
There are a number of reasons why things might not be responding to your touches. Here are two that have happened to me:
The frame of the view you want to touch is too small. UIViews can draw outside of their frames, so it might look ok, but not respond if the touch is technically outside of the frame -- you also have to check that all the superview's up the hierarchy also have a large enough frame.
If anything in your view is a UIImageView or child thereof, it won't respond to user touches because UIImageView has userInteractionEnabled set to NO by default. You can fix this just by setting myImageView.userInteractionEnabled = YES;
Edit: Oli pointed out in the comments that dismissModalViewControllerAnimated: should work if called on either self.parentController or simply self, since that method is smart enough to call the parent if needed, according to the docs. The docs also make it sound like this might behave differently if you have multiple model views open at once, though, so I would still consider it cleaner code to call the method on self.parentController directly.