Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace current viewController with a new viewController

I'm trying to replace my current viewController with a new one. I've been able to do this before but I'm having some issues with BAD_ACCESS.

This is the code that will run when I want to replace the current view with a new one.

(The function will be called using a local property "self.some_data" (nonatomic, retain))

-(void) labelSelected:(SomeDataObject*) some_data{ 
   SomeViewController *viewController = (SomeViewController*)[[ClassManager sharedInstance] viewControllerForClassIdentifier:@"com.somename" fromPlistFileName:@"iPhoneScreenList"];


   viewController.data = (NSObject*)some_data;
   [some_data retain];

   //[self.navigationController pushViewController:viewController animated:YES];

   UINavigationController *tempNavigationController = self.navigationController;

   [[self retain] autorelease];

   [tempNavigationController popViewControllerAnimated:FALSE];
   [tempNavigationController pushViewController:viewController animated:TRUE];
}

Here everything works fine. The issue is that if I release the new "viewController" it crashes. And if I choose:

[tempNavigationController popViewControllerAnimated:TRUE];

I get some really wierd behaviour where the controller never gets replace and I return to the rootController and the navigation bar has two layers of text on it.

And if I do this:

[tempNavigationController pushViewController:viewController animated:FALSE];

I get BAD_ACCESS and the application chrashes. It worked before but not anymore.

What am I doing wrong?

Thanks!

like image 892
chikuba Avatar asked Aug 20 '12 08:08

chikuba


People also ask

How do I create a new ViewController?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.

Can any UIViewController push another?

Yes, you can push or present another view controller without any action. You just need to call the push or present code where you want to trigger it.

How do I rename a ViewController?

To change the name of the view controller follow the simple steps. Step 2: Press and hold the control key on the keyboard and press the right click of the mouse. Step 3: A drop-down menu will occur then select refactor in the dropdown. After selecting refactor another dropdown menu will occur select Rename in that.


1 Answers

Use category for controller replace:

//  UINavigationController+ReplaceStack.h


@interface UINavigationController (ReplaceStack)

- (void) replaceLastWith:(UIViewController *) controller;

@end

//  UINavigationController+ReplaceStack.m

#import "UINavigationController+ReplaceStack.h"

@implementation UINavigationController (ReplaceStack)

- (void) replaceLastWith:(UIViewController *) controller {
    NSMutableArray *stackViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [stackViewControllers removeLastObject];
    [stackViewControllers addObject:controller];
    [self setViewControllers:stackViewControllers animated:YES];
}

@end
like image 59
NeverBe Avatar answered Sep 28 '22 15:09

NeverBe