似乎无法删除UIView?

时间:2014-04-09 00:35:03

标签: ios objective-c uiview uinavigationcontroller

毫无疑问,这是另一个愚蠢的问题,因为我再一次错过了一些基本的东西。

我的头文件中有以下内容:

#import <UIKit/UIKit.h>
#import "CRPUserNameViewController.h"
#import "CRPLegalViewController.h"
#import "CRPMainInformationViewController.h"

@interface CRPInitialOpenViewController : UIViewController

@property (strong, nonatomic) CRPUserNameViewController *vcUsername;
@property (strong, nonatomic) CRPLegalViewController *vcLegal;
@property (strong, nonatomic) CRPMainInformationViewController *vcMain;
@property (strong, nonatomic) UINavigationController *ncMain;

@end

我只是尝试加载Navigation Controller并使用以下代码成为rootViewController替换当前UIViewController

self.view.backgroundColor = [UIColor blueColor];

_vcMain = [[CRPMainInformationViewController alloc] initWithNibName:nil bundle:nil];
_ncMain = [[UINavigationController alloc] initWithRootViewController:_vcMain]

self.view.window.rootViewController = _ncMain;
[self.view.window addSubview:_ncMain.view];

不应该那么难!它似乎以前工作!即使我添加:

[self.view removeFromSuperview];

我看不到Navigation Controller。我添加蓝色背景只是为了确定它是否仍在那里!

我错过了什么再一次让我发疯?如何使用Core Data毫无困难,但无法显示Navigation Controller。感叹!

提前致谢。

3 个答案:

答案 0 :(得分:1)

您需要在AppDelegate中执行此操作,您可以设置rootView。如果你在UIViewController中执行它,你可能正在从root设置一个root。

答案 1 :(得分:0)

如果您使用视图控制器,请不惜一切代价手动添加或删除view,并尝试使用更高级别的方法,例如切换UIWindow的{​​{1}}。

优点:

  • SDK将负责为您添加/删除/调整实际视图。
  • 视图控制器将自动保留。
  • 方向已更改,rootController和所有其他与控制器相关的事件将被正确调用。

答案 2 :(得分:0)

事实证明,我已将代码放在窗口尚未实例化的地方。所以,这就是我所有问题的原因。谢谢你提供的所有答案。他们中的一些确实帮了...

虽然仍然需要弄清楚何时使用ViewDidLoad和ViewWillLoad ......

相关问题