objective-c 如何推送视图控制器(视图控制器)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1644293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:26:09  来源:igfitidea点击:

How to push viewcontroller ( view controller )?

iphoneobjective-ccocoa-touchmemory-managementuiviewcontroller

提问by Madhup Singh Yadav

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.

内存管理是 iPhone 中一个非常重要的问题。所以我问了一个非常笼统的问题。有两种方法可以调用另一个类的viewController。

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?

所以显而易见的问题是,调用其他类的 viewController 的最佳方式是什么?方式一还是方式二?

Suggestions and comments are openly invited.

公开征求建议和意见。

Please comment and vote.

请评论并投票。

采纳答案by Kristopher Johnson

Way 1 is simpler.

方式1更简单。

Way 2 lets the first controller keep a reference to the pushed view controller. If you need that reference, then this would be useful.

方式 2 让第一个控制器保持对推送视图控制器的引用。如果您需要该参考,那么这将很有用。

There is no clear answer here. It depends upon your needs. The general rule, of course, is to make the code as simple as possible, but no simpler.

这里没有明确的答案。这取决于您的需求。当然,一般规则是使代码尽可能简单,但不能更简单。

回答by Morion

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

嗯......为了让事情简单,为什么不只是:

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

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