ios – 连接在一起:AppDelegate,ViewController和XIB

我是iOS编程的新手,遇到了一个问题.

做了什么:

我已经创建了iOS应用程序.
最初它有main.m,AppDelegate.h,AppDelegate.m和一些其他通常创建的支持文件(不带代码).

然后我手动创建带接口(LoginView.xib)的XIB文件,并手动添加LoginViewController.h和LoginViewController.m来控制XIB.

为LoginViewController.h添加了插座.

毕竟我设置了文件的所有者类(LoginViewController)并在XIB和LoginViewController.h之间建立了连接.

问题描述:

我需要在应用程序启动时立即显示实例化登录视图控制器并显示登录视图.

我尝试了几次尝试并阅读了十几个论坛帖子,但没办法.除了白色窗口背景外没有显示任何内容.
我怎么能这样做?

参考代码:

LoginViewController.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController
{
    IBOutlet UIView        *_loginView;
    IBOutlet UITextField   *_tfLogin;
    IBOutlet UITextField   *_tfPassword;
    IBOutlet UIButton      *_btnLoginToSystem;
}

- (IBAction)attemptLogin:(id)sender;

@end

LoginViewController.m

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)attemptLogin:(id)sender
{

}

@end

AppDelegate.h

#import <UIKit/UIKit.h>
#import "LoginViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong,nonatomic) UIWindow *window;
@property (strong,nonatomic) LoginViewController *loginViewController;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
    [self.window.rootViewController presentModalViewController:self.loginViewController animated:NO];

    [self.window makeKeyAndVisible];
    return YES;
}

UPDATE!
伙计们,多亏你们,我发现了另一个问题.一旦我批准我的代码(当然在适当的版本之后)是正确的,我启动了模拟器并看到我的应用程序崩溃,但有以下异常:

NSInternalInconsistencyException with the reason [UIViewController _loadViewFromNibNamed:bundle:] loaded the ... nib but the view outlet was not set.

感谢StackOverflow,我修复了它.
Loaded nib but the view outlet was not set – new to InterfaceBuilder

Josh Justice的评论从我提供的主题链接到:

  1. Open the XIB file causing problems
  2. Click on file’s owner icon on the left bar (top one,looks like a yellow outlined box)
  3. If you don’t see the right-hand sidebar,click on the third icon above “view” in your toolbar. This will show the right-hand sidebar
  4. In the right-hand sidebar,click on the third tab–the one that looks a bit like a newspaper
  5. Under “Custom Class” at the top,make sure Class is the name of the ViewController that should correspond to this view. If not,enter it
  6. In the right-hand sidebar,click on the last tab–the one that looks like a circle with an arrow in it
  7. You should see “outlets” with “view” under it. Drag the circle next to it over to the “view” icon on the left bar (bottom one,looks like a white square with a thick gray outline
  8. Save the xib and re-run

将这些信息汇总到一个点可能会帮助其他新手以更快的速度通过.

解决方法

在您的应用程序中将LoginViewController设置为根视图控制器:didFinishLaunchingWithOptions:方法.
self.window.rootViewController = self.loginViewController;

删除此行:

[self.window.rootViewController presentModalViewController:self.loginViewController animated:NO];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...