如何制作 RootViewController
How to make RootViewController
我想通过编码制作导航控制器但是有一个问题:下面是我的代码
这是Appdelegates.m
#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;
使用 root 和导航控制器实现,不带故事板,仅使用 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 的常规项目设置中,您可能需要删除 "main interface"。如果您不想要默认故事板,也可以删除它并清理您的项目。并从您的模拟器或设备中删除该应用程序,然后重新构建它。
@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 加载视图,而只是创建 class.
的实例
尝试初始化控制器:
ViewController *firstVC = [[ViewController alloc] initWithNibName:@"LaunchScreen" bundle:nil];
其中 LaunchScreen
是主 RootController 的 Xib 名称(您称为 ViewController
的那个)。如果xib准备好,ViewDidLoad方法就可以了。
我想通过编码制作导航控制器但是有一个问题:下面是我的代码
这是Appdelegates.m
#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;
使用 root 和导航控制器实现,不带故事板,仅使用 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 的常规项目设置中,您可能需要删除 "main interface"。如果您不想要默认故事板,也可以删除它并清理您的项目。并从您的模拟器或设备中删除该应用程序,然后重新构建它。
@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 加载视图,而只是创建 class.
的实例尝试初始化控制器:
ViewController *firstVC = [[ViewController alloc] initWithNibName:@"LaunchScreen" bundle:nil];
其中 LaunchScreen
是主 RootController 的 Xib 名称(您称为 ViewController
的那个)。如果xib准备好,ViewDidLoad方法就可以了。