Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push viewcontroller ( view controller )?

Memory management is a very important issue in iPhone. So I am asking a very general question. There are two ways to call a the viewController of another class.

Way 1:

AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil] autorelease];

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

Way 2:

    #import "AnotherClassViewController.h"

    @interface ThisClassViewController : UIViewController{

      AnotherClassViewController *myViewController;

    }

    @property (nonatomic, retain) AnotherClassViewController *myViewController;

    @end

    @implementation ThisClassViewController

    @synthesize myViewController;

    - (void) pushAnotherViewController{

    if(self.myViewController == nil){

    AnotherClassViewController *tempViewController = [[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil];

    self.myViewController = tempViewController;

    [tempViewController release];
    }
    [self.navigationController pushViewController:myViewController animated:YES];
    }

- (void)dealloc{
self.myViewController = nil;
}
@end

So the obvious question is, which is the best way to call the viewController of other class ? Way1 or Way2?

Suggestions and comments are openly invited.

Please comment and vote.

like image 857
Madhup Singh Yadav Avatar asked Oct 29 '09 14:10

Madhup Singh Yadav


People also ask

How do I push a presented ViewController?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.

How do I get ViewController from view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

How do I push a view in SwiftUI?

Allowing to push a new screen onto a navigation stack is as simple as wrapping your SwiftUI views into a NavigationLink wrapper. As long as you contain your views in a navigation view, you'll be able to push new destination views.


1 Answers

Hmm... To keep things simple, why not just:

MyViewController* viewController = [[MyViewController alloc] init];

[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
like image 70
Morion Avatar answered Oct 13 '22 00:10

Morion