Ios 如何制作RootViewController 我想通过编码制作导航控制器,但有一个问题:下面是我的代码
这是appdelegations.mIos 如何制作RootViewController 我想通过编码制作导航控制器,但有一个问题:下面是我的代码,ios,uinavigationcontroller,Ios,Uinavigationcontroller,这是appdelegations.m #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *firstVC = [[ViewController alloc] init ];//WithNibName:@"LaunchScreen" bundle:nil];
UINavigationController *naviCtrl = [[UINavigationController alloc] initWithRootViewController:firstVC];
naviCtrl.navigationBarHidden = NO;
self.window.rootViewController = naviCtrl;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
这是我的rootviewController.m:-
#import "ViewController.h"
#import "DetailsViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark
#pragma mark ViewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
// UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray ];
// indicator.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
// indicator.hidesWhenStopped = NO;
// indicator.alpha = 1.0;
// [indicator startAnimating];
// [self.view addSubview:indicator];
// [self performSelector:@selector(stopActivity:) withObject:nil afterDelay:8];
// Do any additional setup after loading the view, typically from a nib.
[self CreateView];
}
-(void)stopActivity:(UIActivityIndicatorView *)ActivityIndictor{
[ActivityIndictor removeFromSuperview];
}
#pragma mark
#pragma mark CreateView Method
-(void)CreateView{
int space = 3;
UIScrollView *scrollview=[[UIScrollView alloc]init];
[scrollview setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollview setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];
[scrollview setContentOffset:CGPointMake(0, 0)];
//[scrollview setBackgroundColor:[UIColor greenColor]];
[scrollview setScrollEnabled:YES];
[scrollview setShowsHorizontalScrollIndicator:NO];
[scrollview setShowsVerticalScrollIndicator:NO];
[[self view] addSubview:scrollview];
//scrollview.backgroundColor = [UIColor greenColor];
}
但是有一个问题,在我的视图中,控制器viewdidload不工作。…这里是
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions;
使用根目录和导航控制器(不带故事板)实现,仅使用nib文件:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
LoginViewController *newRootViewController = [[LoginViewController alloc]initWithNibName:@"LoginView" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:newRootViewController];
newRootViewController.view.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//Preload keyboard to delete delay on first appereance in UITextFields
UITextField *delayFreeField = [[UITextField alloc] init];
[self.window addSubview:delayFreeField];
[delayFreeField becomeFirstResponder];
[delayFreeField resignFirstResponder];
[delayFreeField removeFromSuperview];
return YES;
按照习惯设置您的属性 此外,在常规项目设置的xcode中,您可能需要删除主界面。如果不需要,也可以删除默认情节提要并清理项目。并从模拟器或设备中删除应用程序,然后重新构建它
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) UINavigationController *navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
navController = [[UINavigationController alloc] init];
[navController pushViewController:viewController animated:NO];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
问题是您的RootController是用以下代码行初始化的:
ViewController *firstVC = [[ViewController alloc] init];
ViewDidLoad方法不起作用,因为您没有从Nib加载视图,而只是创建了一个类的实例
尝试使用以下命令初始化控制器:
ViewController *firstVC = [[ViewController alloc] initWithNibName:@"LaunchScreen" bundle:nil];
其中LaunchScreen是主RootController的Xib的名称,您称之为ViewController。如果xib准备好了,ViewDidLoad方法就可以工作。我可以简单地初始化我的视图控制器吗?当然可以!该代码段是无延迟UITextField的附加代码。。。它来自一个实际工作的应用程序。但当我这样做时,我会遇到一些问题,就像rootViewcontroller中的viewdilad不工作rootViewcontroller工作不正常一样。只需使用一个通用的视图进行测试。不,问题是如果您只是“初始化”它,您就不会加载视图。您需要使用xib文件对其进行初始化,以使viewDidLoad工作。为什么要使用[navController-pushViewController:viewController-animated:NO];?它会将屏幕上的新控制器推到应用程序主视图上,而不是直接将其放在主视图上。有了这段代码,你的控制器基本上会从屏幕的底部弹出到顶部。我想让应用程序中的根视图控制器成为代理,而不是推式视图控制器,,,,,以及为什么我需要这样做……看看他的原始问题和原始代码。他正在实例化导航控制器和视图控制器。因此我认为这就是他想要做的。另外,关于您关于推送视图控制器的问题。。。initWithRootViewController:UIViewController*rootViewController。。。。。。。从docs中,这是初始化接收器并将根视图控制器推送到导航堆栈上的一种方便方法。每个导航堆栈必须至少有一个视图控制器作为根。