Back and forth, removeFromSuperView - iphone

Im a bit confused...
I have a mainview, which by the click of a button shows a subview. What I want to do next is to
be able to switch back and forth between the Main and the Sub, but when I do this, the return button FROM the subView makes the TO SubView button disabled...I cant seem to be able to access the Subview a second time. Nothing happens.
What I have is a UIButton, and an IBAction, which I have written the "[self.view removeFromSuperView];" in.
Please do not get me wrong here, I do get back to the mainView. The problem is that I cant go back to the subview afterwards.
I've read something in the documentation about retaining the subView...But I did not quite understand how to do it.
Could anyone please shed some light over this?
Thanks!

this is just a guess, since you don't provide much code:
- you might first need to retain the view. in case it's only retained by its current superview
[self.view retain];
remember to free it manually when it's no longer needed.
[self.view release];

Why dont you try out this?
to open ur subview:
Ursubview *Ursubview =[[Ursubview alloc] initWithNibName:nil bundle:nil];
Ursubview.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:Ursubview animated:YES];
to go back:
[self dismissModalViewControllerAnimated:YES];

Why not just hide it instead of removing it?

Related

When trying to view table view after viewing another, app crashes

i have a tough one for you today. I have two tableViews in my app the first is on the first page. There are two other pages the user drills down to get to the second table view. After i leave the first table view, i can press the back buttons to get back perfectly until i reach the second table view. As soon as i drill down to the second table view and then try to return to the first via pressing the back buttons. As soon as i get to the last back button to return to the first table view, the app crashes. The code for the back buttons is simply:
- (IBAction)goBack:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
Any Help Would be greatly appreciated!! Thanks everyone!! :D
Whenever I create a modal view controller from a XIB, the automatic #property generator duplicates things in the Dealloc method, thus throwing an EXC_BAD_ACCESS when the view is dismissed. Make sure you aren't releasing something twice.
Sounds like your are releasing something too early. Open you app in instruments (command + i) and run a zombie test.
As soon as you see zombie has been messaged expand the right panel and have a look at the user code (your code) blocks. Indicated by the back person icon.
Double click that and it will indicate what it was trying to access that had already been released.
Are your tableviews being displayed in a modal window? If not, why are you calling [self dismissModalViewControllerAnimated:YES]? Shouldn't you be calling [[self navigationController] popViewControllerAnimated:YES]?
If you're using a UINavigationController, the back button functionality should be provided automatically.
if you are using [[self navigationController] popViewControllerAnimated:YES] to
then for back you write as mentioned below:
(IBAction)goBack:(id)sender {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
if you are using [self presentmodalviewcontroller: animated:]
then only [self dismissModalViewControllerAnimated:YES] will work
you try this [[self navigationController] popViewControllerAnimated:YES]

Presenting a ModalViewController inside a ModalViewController

I have a view which is presented as a modal view controller which takes username and password credentials. I want this view to check the delegate, and if the user hasn't previously set an unlock pin for the app, to then show the change pin view as a modal view controller. This is my code...
+(void)presentCredentialsViewController:(UIViewController *)vc{
CredentialsViewController *cvc = [[CredentialsViewController alloc] init];
[vc presentModalViewController:cvc animated:FALSE];
}
and then in CredentialsViewController
-(void)viewDidLoad{
[super viewDidLoad];
if([appDelegate.pin isEqualToString: #""]){
UserPrefsViewController *upvc = [[UserPrefsViewController alloc] init];
upvc.cancelButton.hidden = true;
[self presentModalViewController:upvc animated:FALSE];
}
}
But for some reason it doesn't work. The debugger steps through the code without error, never the less, the second modal view controller isn't displayed.
First, I would suggest checking that your appDelegate.pin is blank and not nil. If it is nil, the if statement would not be satisfied and your second ModalView would not be presented.
You may also want to try the previous suggestion, calling presentModalViewController from viewDidAppear, or setting a delay if leaving it in viewDidLoad. It is possible that the CredentialsViewController is trying to present the second view when it has not yet presented itself.
The if statement is being hit and the second PresentModalViewController is executing without error, but it just wasn't displaying. I did try putting the code in ViewDidAppear and a load of other places as well, such as applicationWillBecomeActive etc. Although not actually crashing the code, still none of these approaches would show the view controller. In the end I have opted for this:
start with pin of #""
on applicationDidEnterBackground check if pin has been set
if yes
PresentModalViewController: PinViewController
if no
do nothing
Bit of a hack but it will do for now. I suppose I should put some sort of notification in somewhere warning that the pin hasn't been set. The suggestion about the delay may possibly work I suppose. I might give it a go in the future. Thanks guys....points up!

uiview in navigation

I am working with a navigation application. I have a homeViewController with two views(accept, service). When I click on a button in serviceView it should go to acceptView. I should have back button in navigation bar which takes me be back to serviceView from acceptView. When I am trying to use [self navigationController pushViewController, it only accepts viewcontroller but not view. Can any one please tell the alternative. Please help me.
Thanks in advance
You should have a different viewController for each view if you wish to use a navigationController properly.
Set up AcceptViewController and ServiceViewController separately. Then, from the AcceptViewController, you can create a ServiceViewController and push it onto the stack as follows:
-(void)showServiceView {
ServiceViewController *serviceViewController = [[ServiceViewController alloc] init];
[self.navigationController pushViewController:serviceViewController];
[serviceViewController release];
}
Assuming you've references to both acceptView and serviceView, you can just make this work by removing one as the subview and adding the other one as the subview of homeViewController's view. Something like,
[serviceView removeFromSuperview];
[self.view addSubview:acceptView];
for moving to acceptView. Switch them if you want to come back. However this mechanism will be abrupt. Use UIView's transitionFromView:toView:duration:options:completion: method to animate the transition. Something like,
[UIView transitionFromView:serviceView
toView:acceptView
duration:0.5f
options:UIViewAnimationOptionTransitionFlipFromLeft| UIViewAnimationOptionCurveEaseInOut
completion:NULL];
This will remove serviceView and add acceptView as the subview along with a transition to go by.

Releasing a view controller after a CATransition: Am I doing this right?

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

Why does animating a modal view controller onscreen cause a crash here?

This piece of code works fine. However, if I change the animated parameter to YES it crashes.
AccountViewController *accViewController = [[AccountViewController alloc] initWithNibName:#"Account" bundle:nil];
[self.navigationController pushViewController:accViewController animated:NO];
[accViewController release];
What could be wrong?
Are you doing this while any other animation is going on? I've seen crashes here in that type of situation (for example, while one view is being dismissed, pushing or presenting another will crash.)
Try dropping the release call. Although I would assume that push should retain, I can't recall for sure and i am not sure when it retains.