如何制作RootViewController

时间:2015-01-13 08:58:48

标签: ios uinavigationcontroller

我想通过编码制作导航控制器,但有一个问题:下面是我的代码

这是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无效......

3 个答案:

答案 0 :(得分:0)

以下是

完整实现的代码段
- (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;

答案 1 :(得分:-1)

问题是你使用这行代码初始化了RootController:

ViewController *firstVC = [[ViewController alloc] init];

ViewDidLoad方法不起作用,因为您没有从Nib加载视图,而只是创建类的实例。

尝试使用以下命令初始化控制器:

ViewController *firstVC = [[ViewController alloc] initWithNibName:@"LaunchScreen" bundle:nil];

其中LaunchScreen是主RootController的Xib名称(您称之为ViewController的名称)。如果xib准备充分,ViewDidLoad方法将起作用。

答案 2 :(得分:-5)

设置你的属性但是你习惯了 在一般项目设置中的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;
}