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

这是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;


在没有情节提要的情况下,仅使用nib文件即可使用root和导航控制器实现:

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;

关于ios - 如何制作RootViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27918242/

10-12 15:07